How to find out when installation is complete

I am creating an application that installs applications downloaded from the server. I would like to install this application. After downloading the file, the code for the method that 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 the apk file to be deleted from the SD card after installation is complete. This code deletes it after the installation starts, which causes the installation to fail. I am pretty friends with android and will be very grateful for the help. Basically, I try to wait for the installation to complete before continuing with the process.

+3
source share
2 answers

This may 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, . , .

+4

( /) .

, , . .

, :

:

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

Source: https://habr.com/ru/post/1795932/


All Articles