android 安装后自启动 和 静默安装后自动重启

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/InnovationAD/article/details/81454884

安装后自启动很久之前的问题了 今天刚好遇到 记录一下:添加两行命令就搞定了

一般的安装

 Intent intent = new Intent();
        //执行动作
        intent.setAction(Intent.ACTION_VIEW);
      
        //执行的数据类型
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

        context.startActivity(intent);
     

安装后自启动:添加两行命令就搞定了

   Intent intent = new Intent();
        //执行动作
        intent.setAction(Intent.ACTION_VIEW);

        //安装好后打开新版本
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //执行的数据类型
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intent);
        
        //提示完成打开
        killProcess(myPid());

静默安装:

public static void installSlient(Context context, File apk) {

        String cmd = "pm install -r " + apk.getPath();
        Process process = null;
        DataOutputStream os = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        try {
            //静默安装需要root权限
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.write(cmd.getBytes());
            os.writeBytes("\n");
            os.writeBytes("exit\n");
            os.flush();

            //执行命令
            process.waitFor();

            //获取返回结果
            successMsg = new StringBuilder();
            errorMsg = new StringBuilder();

            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) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Log.e(TAG, "=================== successMsg: " + successMsg.toString() + ", errorMsg: " + errorMsg.toString());
        
        //安装成功
        if ("Success".equals(successMsg.toString())) {

            Log.e(TAG, "======= apk install success");

        }

        installApk(context, apk);
    }

具体解析可以到这里看下:https://blog.csdn.net/fuchaosz/article/details/51852442

以上的方法是可以安装成功的 但是会发现安装成功后程序并没有重启。

其实道理也很简单因为在安装的时候执行了命令 process.waitFor(); 这句会把程序kill掉 所以在这句之后的逻辑不会继续往下执行,所以重启的操作要放到process.waitFor();之前。

重启想到了两种方法:

一种是通过其他的app来检查是否安装成功,如果安装成功则直接调起程序,完成重启操作。

另外一种就是通过android给我们提供的闹钟定时器AlarmManager 来重启app

这里主要讲第二种方法:直接上代码  里面我还处理了一下版本的适配问题

Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {// 6.0及以上
            mgr.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, restartIntent);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
            mgr.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, restartIntent);
        }

PendingIntent 可以看做是一个Intent的封装 这里主要做重启app的工作 有兴趣的可以去了解一下 这里不做详细解析了

AlarmManager 定时器  获去操作很简单,也不详细说了。

setExactAndAllowWhileIdle(int type, long Time, PendingIntent pi)

long time 执行时间=当前系统时间+延迟时间 (这个时间是更具安装app大小来判断) 这里应该还有其他的解决方案

以上就是静默安装后重启的方案了!如果还有其他的方法的 欢迎分享!!!!!!!!!!!!!!!!!

有什么问题可以留言!会在最短的时间内答复

猜你喜欢

转载自blog.csdn.net/InnovationAD/article/details/81454884