Install APK programmatically on Android

I am trying to get the Android application to install the APK on sdcard programmatically, but I ran into some small problems.

Here is how I do it:

Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType("ApkFilePath...","application/vnd.android.package-archive"); activity.startActivityForResult(intent,5000); 

Now that everything is working fine, it starts the package manager, and I can control what to do when the manager finishes installing the APK.

But the problem is that if at the end of the installation the user clicks "Open" instead of "Done", the "OnActivityResult" method is not called, because the manager still exists. and this is another problem for another system requirement.

Is there a way to find out when the user chose β€œOpen” at the end of the package manager, or is there a way to make the dispatcher display only the buttons that I want to display?

In fact, you can use the help, I'm looking everywhere and do not see a solution

+6
source share
1 answer

You can add the receiver to your AndroidManifest.xml to listen to the broadcasts if a new application is installed. Like this:

 <receiver android:name=".PackageReceiver"> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_CHANGED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="package" /> </intent-filter> </receiver> 

This class is then called when a new package is installed:

 public class PackageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // handle install event here } } 
+8
source

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


All Articles