Piyush Agarwal
Piyush Agarwal

Reputation: 25858

Result Code is always 0 as result of uninstallation Intent in android

From my Application I am trying to uninstalling an application by using the code

Intent uninstallIntent = new Intent(Intent.ACTION_DELETE);
uninstallIntent.setData(Uri.parse("package:" +packageName));
uninstallIntent.setAction(Intent.ACTION_VIEW);
startActivityForResult(uninstallIntent,UNINSTALL_APPLICATION);

as per I am starting the activity for result I want to perform different actions on the basis of user's input like if cancelled or clicked on ok.

As I expected the result code of clicking ok will be RESULT_OK and clicking on cancel will be RESULT_CANCEL, but in actual in both cases I am getting RESULT_CANCEL.

So how can I differentiate the user's input.

Thanks!

Upvotes: 6

Views: 4100

Answers (3)

Piyush Agarwal
Piyush Agarwal

Reputation: 25858

After a lot of thoughts in mind finally i have got the solution tricky one.

How I Implemented at the time of calling the Uninstall Intent i have saved the package name in a preferences file by using

SharedPreferences prefs;
prefs.edit().putString(DELETE_PACKAGE_NAME, packageName).commit();

And what i have done in OnActivityresult i have just checked whether the application with saved package name still present if it is means user has clicked on Cancel else he clicked Ok.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); 

 if(requestCode==DELETE_APP){

    String requestedPackageName=prefs.getString(DELETE_PACKAGE_NAME, "");

    boolean isPresent=GCMIntentService.isAppPresent(requestedPackageName, this);

    if(isPresent){

      //user Clicked on Cancel
    }else{

      //user Clicked on Ok
    }


 }

}

Code for Checking the application presence

public static boolean  isAppPresent(String packageName,Context context) {


  try{
    ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, 0 );
    return true;

} catch( PackageManager.NameNotFoundException e ){

   return false;
}

 }

Thanks.

UPDATED:

As platform changed a lot since I answered this here is the update

Now you can use an intent where you can explicitly define whether it should return a result or not

val intent = Intent(Intent.ACTION_UNINSTALL_PACKAGE).apply {
            data = Uri.parse("package:$packageName")
            putExtra(Intent.EXTRA_RETURN_RESULT, true)
        }
        startActivityForResult(intent, YOUR_REQUEST_CODE_HERE)

Documentation : https://developer.android.com/reference/android/content/Intent.html#ACTION_UNINSTALL_PACKAGE

This will return a result a

From the documentation :

The returned result code will be Activity.RESULT_OK on success or Activity.RESULT_FIRST_USER on failure.

Starting API 21 Android added a helper method

PackageInstaller.uninstall (String packageName, 
                IntentSender statusReceiver)

Read more about the changes in the link below, stating from Android Q the above updated approach is also deprecated.

https://developer.android.com/reference/android/content/pm/PackageInstaller.html#uninstall(java.lang.String,%20android.content.IntentSender)

Upvotes: 13

Salmon K P
Salmon K P

Reputation: 311

Try this

Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + packageName));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, Constants.FROM_UNINSTALL);

Upvotes: 1

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

Per the API, ACTION_DELETE doesn't return anything. If nothing is returned, then the result status will be RESULT_CANCEL.

Your best bet is to set up an Intent Filter/ Broadcast Receiver listening for the ACTION_PACKAGE_FULLY_REMOVED intent. There is a question on such previously asked here.

Upvotes: 4

Related Questions