2012-01-17 147 views
0

我做了從ftp下載APK的代碼,我試圖在下載後安裝它。我爲Honeycomb寫了這篇文章,所以在每個連接中我都必須使用線程。我如何在線程內的類中使用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 2012-01-17 15:43:00

+0

謝謝,我會的! – Dim 2012-01-17 20:17:05

回答

1

Y您可以同時使用一個處理程序:

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 2012-01-17 15:58:29

0

做到這一點,最徹底的方法是爲你的FTP下載的代碼來通知你的主線程,它已經完成,使主(UI)線程調用startActivity。爲此,您可以使用任意數量的線程間通信方式,例如處理程序:

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

我想你需要通過你的活動的背景下您想要使用活動方法的類。

0

要執行UI方法您已經在UI線程上運行此:

內您的正常線程的run()方法將這個:

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

       startyourActivity(); 
      } 
     });