Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

android - Implicit intent to uninstall application?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...