单击按钮卸载应用程序本身

时间:2017-04-19 16:20:28

标签: java android exception uninstall

我正在尝试通过点击按钮卸载应用程序本身,因为我的应用程序在添加了一些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评论解决的问题

1 个答案:

答案 0 :(得分:0)

使用以下代码

 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);