1. 直接使用Intent卸载

//android自带了一个安装程序---/system/app/PackageInstaller.apk.
//大多数情况下,我们手机上安装应用都是通过这个apk来安装
Uri uri = Uri.fromParts("package", "com.example.demo", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);

2. 通过pm命令方式实现静默卸载

//pm命令可以通过adb在shell中执行,同样,我们可以通过代码来执行
public static String execCommand(String... command) {
    Process process = null;
    InputStream errIs = null;
    InputStream inIs = null;
    String result = "";
    try {
        process = new ProcessBuilder().command(command).start();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read = -1;
        errIs = process.getErrorStream();
        while ((read = errIs.read()) != -1) {
            baos.write(read);
        }
        inIs = process.getInputStream();
        while ((read = inIs.read()) != -1) {
            baos.write(read);
        }
        result = new String(baos.toByteArray());
        if (inIs != null)
            inIs.close();
        if (errIs != null)
            errIs.close();
        process.destroy();
    } catch (IOException e) {

        result = e.getMessage();
    }
    return result;
}

execCommand("pm","uninstall", "packageName");

//生成apk时,要在manifest文件下添加 Android:sharedUserId=”android.uid.system”
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
     package="com.xieyuan.mhfilemanager"  
     android:versionCode="1"  
     android:versionName="1.0"  
     android:installLocation="internalOnly"  
     android:sharedUserId="android.uid.system" >  

3. 使用PackageManager静默卸载

谷歌认为该方法是不安全的行为,因此该接口是@hide的,不是公开的接口,调用此接口需要有系统签名和相应的系统级权限

需要 
<uses-permission android:name="android.permission.DELETE_PACKAGES"/>权限,但<uses-permission android:name="android.permission.DELETE_PACKAGES"/> 是系统级权限,普通APP根本无法获取到,如果在 AndroidManifest.xml 强行加入该权限编译也不会通过, 需要借助Apktool之类工具修改加入此权限.

获得 DELETE_PACKAGES 权限后,定义PackageDeleteObserver实现类,实现packageDeleted方法

private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {  
    private int position;  
    private int mFlag;  

    public PackageDeleteObserver(int index, int flag) {  
        position = index;  
        mFlag = flag;// 0卸载1个包,1卸载N个包 N>1  
    }  

    @Override  
    public void packageDeleted(String arg0, int arg1)  
            throws RemoteException {  
        // TODO Auto-generated method stub 
        Message msg;  
        msg = mHandle.obtainMessage();  
        msg.what = FLAG_DELETE_VIRUS;  
        msg.arg1 = position;  
        msg.arg2 = mFlag;  
        msg.sendToTarget();  
    }  
}
//获取PackageManager 对象,调用deletePackage方法
PackageManager pkgManager = mContext.getPackageManager();  
PackageDeleteObserver observer = new PackageDeleteObserver(currVirus, 1);  
pkgManager.deletePackage(pakName, observer, 0);  
// 也可以用以下方式调用命令实现
Process exec = Runtime.getRuntime().exec("su");
DataOutputStream dataOutputStream = new DataOutputStream(exec.getOutputStream());
dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm uninstall " + str); 
 

转载请注明转自: 听风 , 本文固定链接: Android 程序中卸载其它App的几种方法