Android app 安装卸载

实现android apk 的安装和卸载
1实现apk安装

/**
     * 安装APK文件
     */
    public static void installApk(Context context, String filePath) {
        File apkfile = new File(filePath);
        if (!apkfile.exists()) {
            return;
        }
        // 通过Intent安装APK文件
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
                "application/vnd.android.package-archive");
        context.startActivity(i);
    }
  1. apk 卸载
public static void uninstall(Context context, String packagename) {
        Uri packageURI = Uri.parse("package:"+packagename); 
        Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);     
        context.startActivity(uninstallIntent); 
    }

上面的安装卸载时都会弹出对应的提示框,这个提示框不是所有的项目都需要的,有的时候我们需要在后台执行卸载任务,上面的方法就是不适用了,为了实现不弹出提示框的卸载在网上查了很多资料,可能是我使用的方法问题是都不不行,静默安装和卸载都需要root权限,这个直接就给pass了,后天采用下面的方法完美解决
添加的权限和配置

android:sharedUserId="android.uid.system"  系统权限
添加权限
<uses-permission android:name="android.permission.MASTER_CLEAR" />

卸载

        try {
            L.i("pm uninstall  "+name);
            Runtime.getRuntime().exec("pm uninstall  "+name);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

使用goog的签名方法
签名:java -jar signapk.jar platform.x509.pem platform.pk8 文件名.apk 签名后文件名.apk

猜你喜欢

转载自blog.csdn.net/songmingzhan/article/details/77946476