专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

android开发实现静默安装(root权限)

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:31:11       共计:3611 浏览

android开发实现静默安装(root权限)

方式是将应用设置为内置的系统应用,注意事system/app目录下面,采用copy2SystemApp()方法就可以,注意chmod 777的权限,若是直接将apk拷贝到system/app目录,没有这个权限还是不能静默安装的。

直接贴出工具类:

public class ApkController { /** * 描述: 安装 */ public static boolean install(String apkPath,Context context){ // 先判断手机是否有root权限 if(hasRootPerssion()){ // 有root权限,利用静默安装实现 return clientInstall(apkPath);
        }else{ // 没有root权限,利用意图进行安装 File file = new File(apkPath); if(!file.exists()) return false; 
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
            context.startActivity(intent); return true;
        }
    } /** * 描述: 卸载 */ public static boolean uninstall(String packageName,Context context){ if(hasRootPerssion()){ // 有root权限,利用静默卸载实现 return clientUninstall(packageName);
        }else{
            Uri packageURI = Uri.parse("package:" + packageName);
            Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
            uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(uninstallIntent); return true;
        }
    } /** * 判断手机是否有root权限 */ public static boolean hasRootPerssion(){
        PrintWriter PrintWriter = null;
        Process process = null; try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.flush();
            PrintWriter.close(); int value = process.waitFor(); return returnResult(value);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{ if(process!=null){
                process.destroy();
            }
        } return false;
    } /** * 静默安装 */ public static boolean clientInstall(String apkPath){
        PrintWriter PrintWriter = null;
        Process process = null; try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.println("chmod 777 "+apkPath);
            PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
            PrintWriter.println("pm install -r "+apkPath); // PrintWriter.println("exit");  PrintWriter.flush();
            PrintWriter.close(); int value = process.waitFor(); return returnResult(value);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{ if(process!=null){
                process.destroy();
            }
        } return false;
    } /** * 静默卸载 */ public static boolean clientUninstall(String packageName){
        PrintWriter PrintWriter = null;
        Process process = null; try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
            PrintWriter.println("pm uninstall "+packageName);
            PrintWriter.flush();
            PrintWriter.close(); int value = process.waitFor(); return returnResult(value); 
        } catch (Exception e) {
            e.printStackTrace();
        }finally{ if(process!=null){
                process.destroy();
            }
        } return false;
    } /** * 启动app
     * com.exmaple.client/.MainActivity
     * com.exmaple.client/com.exmaple.client.MainActivity */ public static boolean startApp(String packageName,String activityName){ boolean isSuccess = false;
        String cmd = "am start -n " + packageName + "/" + activityName + " \n";
        Process process = null; try {
           process = Runtime.getRuntime().exec(cmd); int value = process.waitFor(); return returnResult(value);
        } catch (Exception e) {
          e.printStackTrace();
        } finally{ if(process!=null){
                process.destroy();
            }
        } return isSuccess;
    } /** * 将文件复制到system/app 目录
     * @param apkPath 特别注意格式:该路径不能是:/storage/emulated/0/app/QDemoTest4.apk 需要是:/sdcard/app/QDemoTest4.apk
     * @return */ public static boolean copy2SystemApp(String apkPath){
        PrintWriter PrintWriter = null;
        Process process = null;
        String appName = "chetou.apk",cmd; try {
            process = Runtime.getRuntime().exec("su");
            PrintWriter = new PrintWriter(process.getOutputStream());
            cmd = "mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system";
            Log.e("copy2SystemApp", cmd);
            PrintWriter.println(cmd);
            
            cmd = "cat "+apkPath+" > /system/app/"+appName;
            Log.e("copy2SystemApp", cmd);
            PrintWriter.println(cmd);
            
            cmd = "chmod 777 /system/app/"+appName +" -R";
            Log.e("copy2SystemApp", cmd);
            PrintWriter.println(cmd);
            
            cmd = "mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system";
            Log.e("copy2SystemApp", cmd);
            PrintWriter.println(cmd);
            PrintWriter.println("reboot"); //重启 PrintWriter.println("exit");
            PrintWriter.flush();
            PrintWriter.close(); int value = process.waitFor(); return returnResult(value);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{ if(process!=null){
                process.destroy();
            }
        } return false;
    } private static boolean returnResult(int value){ // 代表成功  if (value == 0) { return true;
        } else if (value == 1) { // 失败 return false;
        } else { // 未知情况 return false;
        }  
    }
}
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:Android之发送短信的两种方式 | ·下一条:Android应用发送短信的实现

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有