如何检测用户是否在Intent.ACTION_DELETE上单击了OK或CANCEL?

时间:2017-08-13 13:00:37

标签: android android-intent uninstall

我正在尝试制作一个卸载程序应用程序,这是我用来卸载应用程序的部分:

 Uri uri = Uri.fromParts("package", app.getPackageName(), null);
 Intent intent = new Intent(Intent.ACTION_DELETE, uri);
 startActivity(intent);

当用户单击卸载按钮时,会出现一个确认弹出对话框。有没有办法检查用户是否在对话框中单击了OK或CANCEL?

2 个答案:

答案 0 :(得分:1)

不直接,如ACTION_DELETE is not documented to return anything

但是当您的活动返回到前台时,您可以使用PackageManager并查看该应用是否仍在那里。

答案 1 :(得分:1)

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.