Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect wither the user has clicked OK or CANCEL on Intent.ACTION_DELETE?

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 ?

like image 411
Toni Joe Avatar asked Aug 13 '17 13:08

Toni Joe


2 Answers

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.

like image 54
Toni Joe Avatar answered Nov 15 '22 08:11

Toni Joe


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.

like image 35
CommonsWare Avatar answered Nov 15 '22 09:11

CommonsWare