Android – how to find out when the installation is complete

I am creating an application that installs applications downloaded from the server. I want to install these applications. After downloading files, the code of the method I use 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();

}

I want to delete the APK file from the SD card after the installation is completed. After the installation starts, this code will delete it, resulting in the installation failure. I like Android very much and thank you very much for some help. I basically try to wait for the installation to complete before continuing this process

resolvent:

Android package manager sends various broadcast intentions when installing (or updating / deleting) applications

You can register broadcast receivers so that you will be notified, for example, when a new application is installed

The intentions you may be interested in are:

> ACTION_ PACKAGE_ INSTALL > ACTION_ PACKAGE_ REPLACED > ACTION_ PACKAGE_ CHANGED > ACTION_ PACKAGE_ ADDED

Using a broadcast receiver is not a big problem:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>