Implicit intent to uninstall application?

孤街浪徒 提交于 2019-11-26 15:28:42

问题


I am trying to have an onclicklistener call an intent to uninstall an app, by having the intent call the default "uninstall app" activity from the applications settings. I have found here that I can uninstall an app using ACTION_UNINSTALL_PACKAGE, com.packageXYXY, which seems to be what I'm looking for. However, I am unsure how to call this. I have tried the following:

public void onClick(DialogInterface dialog, int which) {
                Uri packageURI = Uri.parse("package:com.packageName");
                Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
                startActivity(uninstallIntent);

but the syntax is wrong. Have tried a number of different ways of calling this, and am kind of stuck. Not sure how to call this. Thanks for your help.


回答1:


First of all, note that the ACTION_UNINSTALL_PACKAGE is only availible to android-14 (i.e. Ice Cream Sandwich, Android 4.0). That said, the following code works for me:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.net.Uri;
import android.content.Intent;

public class TestActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView view = (TextView)findViewById(R.id.test_view);
        view.setOnClickListener(new View.OnClickListener(){
          public void onClick(View view){
            Uri packageUri = Uri.parse("package:org.klnusbaum.test");
            Intent uninstallIntent =
              new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
            startActivity(uninstallIntent);
          }
        });
    }
}

If you want to be able to do this on all versions of the android platform, just change the intent from Intent.ACTION_UNINSTALL_PACKAGE to Intent.ACTION_DELETE like @goto10 does.




回答2:


Try ACTION_DELETE instead. That's what this example suggests.

EDIT: I just tested this myself and it worked great.




回答3:


In the Api Demos it looks like they are giving the full path to the activty, not just the package itself. This seems weird, because helloactivity activity is not declared in the manifest of that project. So maybe it is just the package path...

However, set the extra EXTRA_RETURN_RESULT to true in your intent, then start the activity for result and check the result code, maybe it will return a code/extra field in the data intent saying what is the error (Read in the documentation for that)



来源:https://stackoverflow.com/questions/7868460/implicit-intent-to-uninstall-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!