Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 579
Piyush Agarwal Avatar asked Dec 21 '12 14:12

Piyush Agarwal


People also ask

What is result code in onActivityResult?

onActivityResult - resultCode is always 0.

What is intent protocol?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.

What is the intent in java?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities.


1 Answers

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)

like image 117
Piyush Agarwal Avatar answered Sep 28 '22 04:09

Piyush Agarwal