如何找出安装完成的时间

Bmo*_*ore 3 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();

}
Run Code Online (Sandbox Code Playgroud)

我想在安装完成后从sd卡中删除apk文件.安装启动后,此代码将删除它,导致安装失败.我非常喜欢android,非常感谢一些帮助.我基本上试图等待安装完成后再继续这个过程.

Leo*_*Leo 12

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

您可以注册广播接收器,因此您将收到通知,例如安装新应用程序时.

您可能感兴趣的意图是:

使用广播接收器并不是什么大问题:

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);
Run Code Online (Sandbox Code Playgroud)

  • @Bmoore好的,你必须使用`"android.intent.action.PACKAGE_INSTALL"或`Intent.ACTION_PACKAGE_INSTALL`.顺便说一句,这是在开始安装时发送的 - ACTION_PACKAGE_ADDED对你来说可能更好;) (2认同)