How can I get the result code when I use "startActivity" to install apk

I installed apk, which was saved in the directory /data/data/package_name/fileswith the codes below:

Uri uri = Uri.fromFile(new File(apkSavingPath));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"application/vnd.android.package-archive");
mContext.startActivity(intent);

I want it to return a result code to tell me if apk was installed successfully or not, and I tried the method startActivityForResult, but it did not work.

In the method, onActivityResultit resultCode always 0(zero)successfully installed apk or not. Can I get such a result?

+5
source share
1 answer

try it.

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

Add your receiver to AndroidManifest.xml

<receiver android:name=".PackageReceiver" android:enabled="true" android:exported="true">
    <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
  }
}
+5
source

All Articles