简体   繁体   English

下载并安装APK

[英]Downloading and installing APK

I made code that download APK from ftp, and I`m trying to install it after download. 我编写了从ftp下载APK的代码,我想在下载后安装它。 I wrote this for Honeycomb, so in every connection i have to use threads. 我是为Honeycomb编写的,因此在每个连接中我都必须使用线程。 How can I use startActivity in class within thread, or wait for thread to finish? 如何在线程中的类中使用startActivity或等待线程完成?

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();
}

} }

You can use a handler: 您可以使用处理程序:

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

        }
    };

When your thread is done running the code it needs to call: handler.sendEmptyMessage(0); 当线程运行完代码后,需要调用: handler.sendEmptyMessage(0);

More info: http://developer.android.com/reference/android/os/Handler.html 更多信息: http : //developer.android.com/reference/android/os/Handler.html

The cleanest way to do this is for your FTP download code to notify your main thread that it has finished, and make the main (UI) thread call startActivity. 最干净的方法是让您的FTP下载代码通知主线程它已经完成,并使主(UI)线程调用startActivity。 You can do this using any number of methods for communication between threads, for example a Handler: 您可以使用多种方法在线程之间进行通信,例如Handler:

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

Or simply Activity.runOnUiThread: 或者只是Activity.runOnUiThread:

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable ) http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable

A good read is the "Painless Threading" blog post: 很好的阅读是“无痛线程”博客文章:

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

我想您需要将活动的上下文传递给您要在其中使用活动方法的类。

To execute UI methods you have run this on UI Thread: 要执行UI方法,您已经在UI Thread上运行了它:

Put this inside your normal Threads run() Method: 将其放入常规线程run()方法中:

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

               startyourActivity();
           }
       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM