0

This is my code for calling the uninstall Intent for a package name which is input through a text field.

The problem is, my app force closes when I press the button, below is my code-

public void uninstall(View view) {
    String packagename = seturl.getText().toString();
    Uri packageUri = Uri.parse(packagename);
    Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE,
            packageUri);
    startActivity(uninstallIntent);
}

logs

    12-24 22:18:43.456: E/AndroidRuntime(31831): FATAL EXCEPTION: main
12-24 22:18:43.456: E/AndroidRuntime(31831): Process: com.example.xadb, PID: 31831
12-24 22:18:43.456: E/AndroidRuntime(31831): java.lang.IllegalStateException: Could not execute method for android:onClick
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.view.View$DeclaredOnClickListener.onClick(View.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.view.View.performClick(View.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.widget.TextView.performClick(TextView.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.view.View$PerformClick.run(View.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.os.Handler.handleCallback(Handler.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.os.Handler.dispatchMessage(Handler.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.os.Looper.loop(Looper.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.ActivityThread.main(ActivityThread.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at java.lang.reflect.Method.invoke(Native Method)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
12-24 22:18:43.456: E/AndroidRuntime(31831): Caused by: java.lang.reflect.InvocationTargetException
12-24 22:18:43.456: E/AndroidRuntime(31831):    at java.lang.reflect.Method.invoke(Native Method)
12-24 22:18:43.456: E/AndroidRuntime(31831):    ... 12 more
12-24 22:18:43.456: E/AndroidRuntime(31831): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.UNINSTALL_PACKAGE dat=com.example.avd VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} }
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.Instrumentation.execStartActivity(Instrumentation.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.Activity.startActivityForResult(Activity.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.Activity.startActivityForResult(Activity.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.Activity.startActivity(Activity.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at android.app.Activity.startActivity(Activity.java)
12-24 22:18:43.456: E/AndroidRuntime(31831):    at com.example.xadb.MainActivity.uninstall(MainActivity.java:61)
12-24 22:18:43.456: E/AndroidRuntime(31831):    ... 13 more
Charuක
  • 12,953
  • 5
  • 50
  • 88
bad mom
  • 61
  • 6

1 Answers1

1

Issue is with the package name,it is not getting the right one,first hardcore the package(if your package name is com.android.yourpakgenamehere ) name and see if it works!

Uri packageURI = Uri.parse("package:com.android.yourpakgenamehere"); // see here how i used it
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);

Then you can get it like this and replace that hardcore name ,

Global to the class:

 public static String PACKAGE_NAME;

Then..

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        PACKAGE_NAME = getApplicationContext().getPackageName();
    }

You can then access it via `Main.PACKAGE_NAME`.  // considering Activity name as Main

Now you have the package name so do this;

  Uri packageUri = Uri.parse("package:"+ActivityYouUse.PACKAGE_NAME);

anyway when you put it on the play store can you change your package name? So even that string value is static

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • It works, but how do I set the package value through my string? my string keeps changing so I don't want to hard code it – bad mom Dec 25 '16 at 05:11
  • I just want to set the package name from my edit text (seturl) – bad mom Dec 25 '16 at 05:17
  • ok if you find it hard get package name like this http://stackoverflow.com/a/6589829/5188159 then `Uri packageUri = Uri.parse("package:"+ActivityYouUse.PACKAGE_NAME);` – Charuක Dec 25 '16 at 05:17
  • @bad mom now you can accept my answer :P if anything is unclear ask me – Charuක Dec 25 '16 at 05:22
  • no it didn't work. Is there a simple solution for using edit text string for getting user package name – bad mom Dec 25 '16 at 13:00
  • @bad mom it should work unless you missed a point. If you really want to add another step ,you can use an edit text.But to get a string first you need to set a string to your edit text.. i dunno why you want that. if you want editText.setText(getApplicationContext().getPackageName()) will do the job then again you need to write Uri packageUri = Uri.parse("package:"+editText.getText().toString()); why you want to do that? – Charuක Dec 25 '16 at 13:05