1

I want to unistall my app by itself but it throws me NullPointerException on SerialVersionUID don't know what's wrong here?

public void UnInstallApplication() throws NameNotFoundException 
{
    Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
            getPackageManager().getPackageInfo("com.XXX.XXX", 0).packageName,null));
            startActivity(intent);
}

or i've used this lines below(with packagename inserted in function) but same result!

Intent uninstallIntent = new Intent(Intent.ACTION_DELETE);
    uninstallIntent.setData(Uri.parse("package:" + packagename));
    startActivity(uninstallIntent); 
Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • 2
    Can you paste the full text of the `NullPointerException`? – Brigham Dec 15 '12 at 15:10
  • exception just threw on debug and it doesn't show on LogCat – Arash GM Dec 15 '12 at 15:19
  • 1
    Then run the app outside of the debugger, so that you get a stack trace in LogCat that you can examine or post here. – CommonsWare Dec 15 '12 at 16:05
  • 1
    When you call methods of objects returned from other methods, you run the risk of an NPE if one of those return values is null. Instead of stacking them up on one line like that in a situation where failure is possible, give the objects names so you can test that they are non-null. It is likely that the result of your getPackageInfo() is null because no package with the (likely slightly wrong) specification you are giving exists. – Chris Stratton Dec 15 '12 at 18:08
  • Thank You Chris for your advice i'll try your words , but i've hardcoded my packagename(which described in manifest) and i got NullPointer,i've solved bunch of NullPointerException before but here is kind of odd to me! – Arash GM Dec 15 '12 at 19:17

4 Answers4

3

use this code:

Uri packageURI = Uri.parse("package:your.packge.name");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27
0

A 3rd party app cannot install or uninstall any other packages programmatically, that would be a security risk for Android. However a 3rd party app can ask the Android OS to install or uninstall a package using intents, this question should provide more complete information:

install / uninstall APKs programmatically (PackageManager vs Intents)

Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • 2
    -1 Please read the code in the question next time - asking Android to begin the uninstall process with an Intent is exactly what is being attempted. – Chris Stratton Dec 15 '12 at 18:06
0

My mistake was that i was calling my uninstall method on a function which was running on doinbackground inside a class that extends AsyncTask , so i've changed my calling method and it prompted me to uninstall my app.

Arash GM
  • 10,316
  • 6
  • 58
  • 76
0

The #1 answer is very near to the correct one.

These code is verified by my recent app:

int REQUEST_UNINSTALL = 1;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_UNINSTALL) {
  // 0 means success, other means failed.
  Log.d(TAG, "got result of uninstall: " + resultCode);
  }
}

void uninstallPackage() {
    Uri packageURI = Uri.parse("package:" + "com.android.xxxx");
    Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
    startActivityForResult(uninstallIntent, REQUEST_UNINSTALL);
}

The onActivityResult will get the uninstall result, and the uninstall package will prompt a system dialog asking user if remove a package.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Jiejing Zhang
  • 1,030
  • 8
  • 16