简体   繁体   English

Android从运行时失败中卸载APK

[英]Android uninstall apk from runtime failure

(using android studio) I want to uninstall an apk from my device, using a runtime. (使用android studio)我想使用运行时从设备上卸载apk。 I don't know why if i install an apk it works, but not if I uninstall. 我不知道为什么要安装apk才能正常工作,但如果我卸载则不能。 I wrote this code: 我写了这段代码:

private int DoAction(String apkPackage) {
    try {
        String res = "";

        Process p = Runtime.getRuntime().exec("su\n");
        DataOutputStream o = new DataOutputStream(p.getOutputStream());
        InputStream i = p.getInputStream();

        o.writeBytes("pm");
        o.writeBytes(" ");
        o.writeBytes("uninstall");
        o.writeBytes(" ");
        o.writeBytes(apkPackage);
        o.writeBytes("\n");
        o.writeBytes("exit");
        o.writeBytes("\n");
        o.flush();
        p.waitFor();
        res = StreamToString(i);

        return p.exitValue();
        }
    } catch (Exception e) {
        return -999999;
    }
}

So the DataOutputStream, would be: 因此,DataOutputStream为:

pm uninstall apkPackage
exit

but the res value is always "FAILURE". 但是res值始终为“ FAILURE”。

I've already added the permission: "android.permission.DELETE_PACKAGES". 我已经添加了权限:“ android.permission.DELETE_PACKAGES”。

For uninstalling any app just pass package name to this method 要卸载任何应用程序,只需将软件包名称传递给此方法

//calling
     DoAction("your package name");

// method definition
     private void DoAction(String apkPackage) {
      try {
           Intent intent = new Intent(Intent.ACTION_DELETE);
           intent.setData(Uri.parse("package:"+apkPackage));
           startActivity(intent);
          }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM