6

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!

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • @SalmonKP answer worked for me. But I had to add **REQUEST_DELETE_PACKAGES** permission first. – Reza Apr 28 '18 at 17:05

3 Answers3

13

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)

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
4

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.

Community
  • 1
  • 1
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • so is any other way to detect this thing ?? – Piyush Agarwal Dec 21 '12 at 14:50
  • See my edit, essentially you want to set up an intent filter listening for `ACTION_PACKAGE_FULLY_REMOVED`. – PearsonArtPhoto Dec 21 '12 at 14:55
  • but in that case we only will get response if user clicked on ok what about cancel?? – Piyush Agarwal Dec 21 '12 at 15:01
  • What I would do is what you have done already, but add the BroadCast Receiver. If you detected it, you know the user hit OK, if you don't detect it, they hit cancel. It would help if you explained why you want to detect if a user hit cancel... – PearsonArtPhoto Dec 21 '12 at 15:02
  • Actually i want to give three attempts to user if he cancels.As you suggested i also thought d some the problem is how i will come to know if user clicks cancel becoz it will not come to the receiver so for implementing this i need to put a timer to check the response came or not receiver. – Piyush Agarwal Dec 22 '12 at 21:54
1

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);
Salmon K P
  • 301
  • 3
  • 9