Android Apk静默安装的方法

最近在开发系统应用,需要在8.0设备上实现静默安装和静默卸载功能,百度了无数次,最后看到一篇文章实现了功能

参考链接 https://blog.csdn.net/qhs1573/article/details/81030567

直接上代码



/**
 * app安装和卸载类
 * 静默安装所需权限
 *     
 *     
 * 静默卸载所需权限
 * 
 * 
 */
public class InstallUtil {


    /**
     * 正常安装
     * @param file
     * @param context
     */
    public static void installApk(Context context,File file) {
        //更新包文件
         Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= 24)
        { // Android7.0及以上版本 Log.d("-->最新apk下载完毕","Android N及以上版本");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file);
    //参数二:应用包名+".fileProvider"(和步骤二中的Manifest文件中的provider节点下的authorities对应)
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
        // Android7.0以下版本 Log.d("-->最新apk下载完毕","Android N以下版本");
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);


    }



    /**
     * 静默安装
     * @param apkPath
     * @return
     */
    public static boolean installClientApp(String apkPath) {

        Process process = null;

        BufferedReader successResult = null;

        BufferedReader errorResult = null;

        StringBuilder successMsg = new StringBuilder();

        StringBuilder errorMsg = new StringBuilder();

        try {
//            //7.0以前版本使用
//            process = new ProcessBuilder("pm", "install", "-r", apkPath).start();
            //7.0以后版本使用
            process = new ProcessBuilder("pm", "install","-i",BuildConfig.APPLICATION_ID, "-r", apkPath).start();

            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));

            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            String s;

            while ((s = successResult.readLine()) != null) {

                successMsg.append(s);

            }

            while ((s = errorResult.readLine()) != null) {

                errorMsg.append(s);

            }

        } catch (Exception e) {



        } finally {

            try {

                if (successResult != null) {

                    successResult.close();

                }

                if (errorResult != null) {

                    errorResult.close();

                }

            } catch (Exception e) {



            }

            if (process != null) {

                process.destroy();

            }

        }

        Log.e("result",""+errorMsg.toString()+",successMsg="+successMsg);

//        Toast.makeText(context,"  "+successMsg , Toast.LENGTH_LONG).show();

        //如果含有“success”单词则认为安装成功

        return successMsg.toString().equalsIgnoreCase("success");

    }


    /**
     * 静默卸载
     * @param context
     * @param pkg
     */
    public static void unInstall(Context context,String pkg){

        Intent intent = new Intent();

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent sender = PendingIntent.getActivity(context, 0, intent, 0);

        PackageInstaller mPackageInstaller = context.getPackageManager().getPackageInstaller();

        mPackageInstaller.uninstall(pkg, sender.getIntentSender());// 卸载APK

    }

}

静默安装方法中,下面这行代码是关键

//            //7.0以前版本使用
//            process = new ProcessBuilder("pm", "install", "-r", apkPath).start();
            //7.0以后版本使用
            process = new ProcessBuilder("pm", "install","-i",BuildConfig.APPLICATION_ID, "-r", apkPath).start();

 

 

 

 

 

你可能感兴趣的:(Android)