单击按钮即可卸载应用程序本身
Posted
技术标签:
【中文标题】单击按钮即可卸载应用程序本身【英文标题】:Uninstall app itself on click of button 【发布时间】:2017-09-16 00:02:15 【问题描述】:我正在尝试通过单击按钮来卸载应用程序本身,因为我的应用程序在添加了一些 Context.DEVICE_POLICY_SERVICE
时无法被用户卸载
try
try
ComponentName devAdminReceiver = new ComponentName(mContext, AdminReceiver.class);
DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.removeActiveAdmin(devAdminReceiver);
catch (Exception e)
e.printStackTrace();
new Handler().postDelayed(new Runnable()
@Override
public void run()
try
Uri packageUri = Uri.parse(getPackageName());
Intent uninstallIntent =
new Intent(Intent.ACTION_DELETE, packageUri);
uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, false);
//new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);
catch (Exception e)
e.printStackTrace();
Uri packageURI = Uri.parse(getPackageName());
Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
startActivity(uninstallIntent);
, 1200);
catch (Exception e)
e.printStackTrace();
错误日志
android.content.ActivityNotFoundException: No Activity found to handle Intent act=android.intent.action.UNINSTALL_PACKAGE dat=zcode.assistive.easy.touch
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1669)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1441)
at android.app.Activity.startActivityForResult(Activity.java:3464)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3425)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
at android.app.Activity.startActivity(Activity.java:3661)
at android.app.Activity.startActivity(Activity.java:3629)
编辑:@CommonsWare cmets 解决的问题
【问题讨论】:
Uri packageUri = Uri.parse(getPackageName());
是错误的。这需要是Uri packageUri = Uri.parse("package:"+getPackageName());
。
【参考方案1】:
使用下面的代码
Uri packageUri = Uri.parse("package:"+getPackageName());
Intent uninstallIntent =
new Intent(Intent.ACTION_DELETE, packageUri);
uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, false);
//new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);
【讨论】:
以上是关于单击按钮即可卸载应用程序本身的主要内容,如果未能解决你的问题,请参考以下文章