如何找出安装完成的时间

时间:2011-03-03 04:26:43

标签: android

我正在创建一个安装从服务器下载的应用程序的应用程序。我想安装这些应用程序下载文件后,我用来安装的方法的代码在这里:

 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文件。安装启动后,此代码将删除它,导致安装失败。我非常喜欢android,非常感谢一些帮助。我基本上试图等待安装完成后再继续这个过程。

2 个答案:

答案 0 :(得分:12)

Android 包管理器会在安装(或更新/删除)应用程序时发送各种广播意图

您可以注册broadcast receivers,这样您就会收到通知,例如何时安装了新的应用程序。

您可能感兴趣的意图是:

使用广播接收器并不是一件大事:

BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do whatever you want to do
    }
};

registerReceiver(myReceiver, new IntentFilter("ACTION"));
unregisterReceiver(myReceiver);

答案 1 :(得分:4)

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

 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内核的速度。需要在循环之前进行休眠,以便包管理器有时间在循环开始之前启动。

相关问题