美文网首页
android root情况下 静默安装并启动

android root情况下 静默安装并启动

作者: 神经病人思路广 | 来源:发表于2020-01-06 17:17 被阅读0次

1.添加权限

 <uses-permission android:name="android.permission.INSTALL_PACKAGES"
        tools:ignore="ProtectedPermissions" />

2.执行代码

    /**
     * 执行具体的静默安装逻辑,需要手机ROOT。
     *
     * @param apkPath 要安装的apk文件的路径
     * @return 安装成功返回true,安装失败返回false。
     */
    public static boolean install(String apkPath, Context context) {
        DataOutputStream dataOutputStream = null;
        BufferedReader errorStream = null;
        try {
            //判断文件是否存在
            File file = new File(apkPath);
            if (!file.exists()) {
                return false;
            }
            String[] nameArray = getPackageNameAndLauncherName(apkPath, context);
            if (nameArray[0] == null || nameArray[1] == null) {
                return false;
            }
            // 申请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();
            //立即执行打开应用命令
            String cmd = "am start -n " + nameArray[0] + "/" + nameArray[1];
            dataOutputStream.write(cmd.getBytes(Charset.forName("utf-8")));
            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")) {
                return false;
            }
        } 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 true;
    }

    /**
     * 获取应用包名,启动activity类名
     * @param apkPath
     * @param context
     * @return
     */
    private static String[] getPackageNameAndLauncherName(String apkPath, Context context) {
        String packageName;
        String className = null;
        PackageInfo packageinfo = context.getPackageManager().getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        //获取包名
        packageName = packageinfo.packageName;

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);

        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = context.getPackageManager()
                .queryIntentActivities(resolveIntent, 0);
        for (ResolveInfo resolveInfo : resolveinfoList) {
            Log.d(TAG, "resolveInfo:" + resolveInfo);
        }
        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packagename.mainActivityname]
            className = resolveinfo.activityInfo.name;
        }
        return new String[]{packageName, className};
    }

相关文章

网友评论

      本文标题:android root情况下 静默安装并启动

      本文链接:https://www.haomeiwen.com/subject/wpbkactx.html