4

On android pie, I want to call the package manager to uninstall my own app. Here is what I am trying:

val uri = Uri.parse("package:$packageName")
val uIntent = Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri)
startActivity(uIntent)

Oddly this is not working. Nothing is being shown in the logcat as well.

I have also tried ACTION_DELETE

val uri = Uri.parse("package:$packageName")
val uIntent = Intent(Intent.ACTION_DELETE, uri)
startActivity(uIntent)

Please tell me what I am doing wrong. This seems a pretty straightforward job. Am I missing any permission or something I need to declare in manifest? Thanks.

SayantanRC
  • 799
  • 7
  • 23

2 Answers2

11

I was missing manifest permission.

<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>

This is probably required for Android 6.0 and above. The code in the question now works perfectly. Tested on Android 9 and Android 10.

SayantanRC
  • 799
  • 7
  • 23
  • I also had the experience that starting the intent didn't work for me with the FLAG_ACTIVITY_NEW_TASK flag (`intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);`) – Rooky May 14 '20 at 08:45
  • It works on Android 12. This method deprecated but there is no alternative yet. – Atakan Yildirim May 06 '22 at 11:18
-1
 Intent intent = new Intent(Intent.ACTION_DELETE);
 intent.setData(Uri.parse("package:com.example.myapplication"));
 startActivity(intent);
Vivek Vinodh
  • 114
  • 1
  • 13
  • 1
    Well, I tried ACTION_DELETE as well. Let me update the question. – SayantanRC Oct 29 '19 at 20:11
  • hmm after doing some Google research, it looks like this ability is not available to 3rd party applications, but there may be some workarounds. https://stackoverflow.com/questions/10483493/delete-my-application-programmatically-android – Vivek Vinodh Oct 29 '19 at 20:16
  • Ok. But I do want to show the confirmation. The question you linked to asks for a way to directly uninstall the app without any confirmation. – SayantanRC Oct 29 '19 at 20:25