如何找出当安装完成 [英] How to find out when an installation is completed

查看:156
本文介绍了如何找出当安装完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建安装从服务器下载的应用程序的应用程序。我想安装这些应用程序后,该文件被下载的$ C $下我使用的安装方法是在这里:

I am creating an application that installs apps downloaded from a server. I would like to Install these application After the file is downloaded the code for the method I am using to install is here:

 public void Install(String name)
{
    //prompts user to accept any installation of the apk with provided name
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File
    (Environment.getExternalStorageDirectory() + "/ContentManager/" + name)), "application/vnd.android.package-archive");
    startActivity(intent);
    //this code should execute after the install finishes
    File file = new File(Environment.getExternalStorageDirectory() + "/ContentManager/"+name);
    file.delete();

}

我想有从SD卡中删除APK文件安装完成后。这code删除它,一旦安装启动,从而导致安装失败。我相当neew到Android,并会多AP preciate一些帮助。我基本上是试图等待安装的过程完成后再继续。

I would like to have the apk file deleted from the sd card after the install is completed. This code deletes it once the install is started, causing the installation to fail. I am fairly neew to android and would much appreciate some help. I am basically trying to wait for the installation to complete before continuing with the process.

推荐答案

这可能不是最好的方式,但我解决了这个问题。这是我的新的code的方法。

This might not be the best way but I solved the problem. Here is my new code for the method.

 public void Install(final String name,View view)
{
    //prompts user to accept any installation of the apk with provided name
    printstatus("Installing apk please accept permissions");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File
    (Environment.getExternalStorageDirectory() + "/ContentManager/" + name)), "application/vnd.android.package-archive");
    startActivity(intent);
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    for(int i=0;i<100;)
    {
        System.gc();
        if(view.getWindowVisibility()==0)
        {
            i=200;
            System.gc();
        }
        try {
            Thread.sleep(500);
            System.gc();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    File file = new File(Environment.getExternalStorageDirectory() + "/ContentManager/"+name);
    file.delete();
}

我创建了一个循环,会等到窗口在前面,让法继续执行。从拖慢系统或Linux内核查杀过程中的垃圾收集和线程睡眠prevents它。循环之前的睡眠是必要的,这样的包管理器有时间来启动循环开始之前。

I created a loop that will wait until the window is in the front to let the method continue executing. The garbage collector and thread sleeping prevents it from slowing down the system or the Linux kernel killing the process. The sleep before the loop is needed so the package manager has time to start before the loop begins.

这篇关于如何找出当安装完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆