I'm trying to make an uninstaller app and this is the part that I use to uninstall an app:
Uri uri = Uri.fromParts("package", app.getPackageName(), null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
When the user clicks the uninstall button, a confirmation popup dialog appear. Is there a way to check if the user has clicked OK or CANCEL in the dialog box ?
Never mind guys, I finally found the solution: instead of ACTION_DELETE, I used ACTION_UNINSTALL_PACKAGE (minimum API 14) and this is the final code:
private void uninstallApps(List<AppModel> apps) {
for (AppModel app : apps) {
Uri uri = Uri.fromParts("package", app.getPackageName(), null);
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri);
// store result
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, 1);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// get result
if(resultCode == RESULT_OK){
Log.d(TAG, "onActivityResult: OK");
}else if (resultCode == RESULT_CANCELED){
Log.d(TAG, "onActivityResult: CANCEL");
}
}
I hope this will help someone.
Not directly, as ACTION_DELETE
is not documented to return anything.
When your activity returns to the foreground, though, you could use PackageManager
and see if the app is still there.
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