使用onclick force close从设备卸载应用程序?

时间:2016-12-25 04:41:43

标签: android exception uninstall

这是我的代码,用于调用通过文本字段输入的包名称的卸载Intent

问题是,当我按button时我的应用程序强制关闭,下面是我的代码 -

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

日志

    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

1 个答案:

答案 0 :(得分:1)

问题是包名,它没有得到正确的,第一个硬核包(如果你的包名是com.android.yourpakgenamehere)名称,看看它是否有效!

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

然后你可以这样得到它并替换那个硬核名称,

全班同学:

 public static String PACKAGE_NAME;

然后..

 @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

现在你有了包名,所以这样做;

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

无论如何,当你把它放在游戏商店时,你可以更改你的包名吗?所以即使是字符串值也是 static