2012-01-17 10 views
0

ftpからAPKをダウンロードするコードを作成しましたが、ダウンロード後にインストールしようとしています。私はハニカムのためにこれを書いたので、すべての接続でスレッドを使用する必要があります。スレッド内のクラスでstartActivityを使用する方法、またはスレッドの終了を待つ方法はありますか?これを行うにはAPKをダウンロードしてインストールする

public class FTPapkDowload { 
protected static final String TAG = "Tablet Development"; 
public FTPClient mFTPClient = null; 
public FTPClient mFtp = null; 

public void Start() { 

Thread apkdowload = new Thread(new Runnable() { 
     @Override 
     public void run() { 

      ftpConnect("mysite", "username", "password",21); 
      Log.d(TAG, "Connected"); 
      ftpDownload("/httpdocs/Shamir/app.apk", "sdcard/Download/app.apk"); 
      ftpDisconnect(); 

      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Download/" + "app.apk")), "application/vnd.android.package-archive"); 
      startActivity(intent); //Here is the problem 


     } 

     //Connection 
     public boolean ftpConnect(String host, String username, 
       String password, int port) { 
      try { 

       mFTPClient = new FTPClient(); 
       mFTPClient.connect(host, port); 

       if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { 
        boolean status = mFTPClient.login(username, password); 

        mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); 
        mFTPClient.enterLocalPassiveMode(); 
        return status; 
       } 
      } catch (Exception e) { 
       Log.d(TAG, "Error: could not connect to host " + host); 
      } 

      return false; 
     } 


     //Downloading 
     public boolean ftpDownload(String srcFilePath, String desFilePath) { 
      boolean status = false; 
      try { 

       FileOutputStream desFileStream = new FileOutputStream(
         desFilePath); 
       status = mFTPClient 
         .retrieveFile(srcFilePath, desFileStream); 
       desFileStream.close(); 
       return status; 
      } catch (Exception e) { 
       Log.d(TAG, "download failed"); 
      } 

      return status; 
     } 



     public boolean ftpDisconnect() { 

      try { 
       mFTPClient.logout(); 
       mFTPClient.disconnect(); 
       Log.d(TAG, "Disconected from FTP on apk Download"); 

       return true; 
      } catch (Exception e) { 
       Log.d(TAG,"Error occurred while disconnecting from ftp server on apk download."); 
      } 

      return false; 
     } 

    }); 

    apkdowload.start(); 
} 

}

+0

人々の信用を与えてください[受け入れます回答](http://meta.stackexchange.com/a/5235/164138)を参照してください。あなたは一つの答えを受け入れていません! – THelper

+0

ありがとう、私はします! – Dim

答えて

1

Y OUは、ハンドラを使用することができます:あなたのスレッドは、それが呼び出す必要があるコードを実行して行われ

private Handler handler = new Handler(){ 
     public void handleMessage(Message msg) 
     { 

     } 
    }; 

handler.sendEmptyMessage(0);

さらに詳しい情報:私はあなたがにあなたの活動のコンテキストを渡す必要があると思いますhttp://developer.android.com/reference/android/os/Handler.html

+0

また、UIスレッドでハンドラを作成してから、ワーカースレッド内で 'handler.post(new Runnable(){...});'を呼び出すこともできます。これが私のやり方です。 – Jakar

0

クリーンな方法は、それが終了したことを、あなたのメインスレッドに通知し、メイン(UI)スレッドコールstartActivityを作るためにあなたのFTPのダウンロードコードのためです。

http://developer.android.com/reference/android/os/Handler.html

それとも単にActivity.runOnUiThread:あなたは、たとえば、スレッド間の通信のための任意の数の方法を使用してハンドラこれを行うことができます

が良いの読み取りがある

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnableを)」無痛スレッディング」のブログ記事:

http://android-developers.blogspot.com/2009/05/painless-threading.html

0

アクティビティのメソッドを使用するクラス。 UIのメソッドを実行するには

0

あなたはUIスレッド上でこれを実行した:通常のスレッド()メソッドを実行内側

はこれを入れて:彼らの努力はあなたを助けるとする

 YourClass.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

       startyourActivity(); 
      } 
     }); 
関連する問題