android 监听软件安装与卸载,打开相关操作

1:卸载第三方应用APK。这个比较简单

// 卸载应用程序
public void unstallApp(String appPackageName) {
    Intent uninstall_intent = new Intent();
    uninstall_intent.setAction(Intent.ACTION_DELETE);
    uninstall_intent.setData(Uri.parse("package:" + appPackageName));
    try {
        startActivity(uninstall_intent);
        showToast("操作完成,请等待软件退出后,手动重启");
    } catch (Exception e) {
        showToast("卸载软件失败:" + e.toString());
    }
}

2:卸载系统APK(前提:手机需要root)

    调用方法,里面的方法换成需要卸载APK的名字就好 ,懒得解释,就是简单的adb命令

    boolean isDel = RootCmd.delEthSettingApk(); showToast(isDel ? "删除成功" : "删除失败");

      1:工具类

      

package com.remove;

import android.util.Log;

import java.io.DataOutputStream;
import java.io.IOException;

public class RootCmd {

    private static boolean mHaveRoot = false;

    /***
     * 判断当前设备有没有权限
     * @return
     */
    public static boolean haveRoot() {
        if (!mHaveRoot) {
            int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测
            if (ret != -1) {
                mHaveRoot = true;
            } else {
            }
        } else {
        }
        return mHaveRoot;
    }

    // 执行命令但不关注结果输出
    public static int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    /***
     * @param command
     * @return
     */
    public static boolean exusecmd(String command) {
        Log.e("cdl", "===========" + command);
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            Log.e("cdl", "======000==writeSuccess======");
            process.waitFor();
        } catch (Exception e) {
            Log.e("cdl", "======111=writeError======" + e.toString());
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static void writeFileToSystem(String filePath, String sysFilePath) {
        exusecmd("mount -o rw,remount /system");
        exusecmd("rm -rf /system/media/boomAnimal.zip");
        exusecmd("chmod 777 /system/media");
        exusecmd("cp  " + filePath + " " + sysFilePath);
    }

    public static boolean writeFileToSystemBin(String filePath, String sysFilePath) {
        exusecmd("mount -o rw,remount /system");
        exusecmd("rm -rf /system/bin/netd");
        boolean isSuccess = exusecmd("cp  " + filePath + " " + sysFilePath);
        exusecmd("chmod 777 /system/bin/netd");
        return isSuccess;
    }

    public static boolean delEthSettingApk() {
        exusecmd("mount -o rw,remount /system");
        boolean isdel = exusecmd("rm -rf /system/app/EthNetSetting.apk");
        exusecmd("chmod 777 /system/app/EthNetSetting.apk");
        return isdel;
    }
}

3:监听软件的的安装与卸载,这里写的是动态注册 ,也可以使用静态注册,注意后边的package属性,必须要加,不加广播收不到   

private void initReceiver() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    filter.addDataScheme("package");
    registerReceiver(receiver, filter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.i(TAG, "=================广播内容:" + action);
        if (action.equals(Intent.ACTION_PACKAGE_ADDED) || action.equals(Intent.ACTION_PACKAGE_REPLACED)) { //
            String packageName = intent.getDataString();
           
        }
    }
};

    方法里面可以获取到当前安装或者卸载的包名,

 

 4:APK的安装,静默安装和常规安装,静默安装需要root,请知情   

/***
 * 静默安装APK
 * @param apkPath
 * @return
 */
public static boolean installApkSlient(String apkPath) {
    boolean result = false;
    File f = new File(apkPath);
    if (f != null && f.exists()) {
        DataOutputStream dataOutputStream = null;
        BufferedReader errorStream = null;
        try {
            // 申请su权限
            Process process = Runtime.getRuntime().exec("su");
            dataOutputStream = new DataOutputStream(process.getOutputStream());
            // 执行pm install命令
            String command = "pm install -r " + apkPath + "\n";
            dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
            dataOutputStream.flush();
            dataOutputStream.writeBytes("exit\n");
            dataOutputStream.flush();
            process.waitFor();
            errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String msg = "";
            String line;
            // 读取命令的执行结果
            while ((line = errorStream.readLine()) != null) {
                msg += line;
            }
            Log.d("TAG", "install msg is " + msg);
            // 如果执行结果中包含Failure字样就认为是安装失败,否则就认为安装成功
            if (!msg.contains("Failure")) {
                result = true;
            }
        } catch (Exception e) {
            Log.e("TAG", e.getMessage(), e);
        } finally {
            try {
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }
                if (errorStream != null) {
                    errorStream.close();
                }
            } catch (IOException e) {
                Log.e("TAG", e.getMessage(), e);
            }
        }
        return result;
    }
    return result;
}

常规安装就比较简单,传递文件路径,启动系统程序

 

/**
 * 安装APK文件
 */
public void installApk(String outpath) {
    try {
        File apkfile = new File(outpath);
        if (!apkfile.exists()) {
            MyToastView.getInstance().Toast(context, "安装文件不存在,请先下载");
            return;
        }
        // 通过Intent安装APK文件
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
                "application/vnd.android.package-archive");
        context.startActivity(i);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(安卓前端,android)