I installed an apk which was saved in directory of /data/data/package_name/files
with 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 resultcode to tell me whether the apk installed successfully or not, and I have tried method of startActivityForResult
, but it didn't work.
On method of onActivityResult
, it's resultCode is always 0(zero)
whether apk installed successfully or not. Can I get such a resultcode?
try this.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType("ApkFilePath...","application/vnd.android.package-archive");
activity.startActivityForResult(intent,5000);
Add your receiver on 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 then gets called when a new package is installed:
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// handle install event here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With