美文网首页
Android 静默安装

Android 静默安装

作者: mapleSeriesX | 来源:发表于2018-12-13 10:23 被阅读0次
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Build;
    import android.util.Log;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.InputStreamReader;
    
    import static android.os.Process.killProcess;
    import static android.os.Process.myPid;
    
    /**
     * Created by maple on 2018/12/13 10:09
     * E-Mail Address:740917401@qq.com
     */
    public class AppUpdate {
        private static String TAG = "AppUpdate";
    
        /**
         * 普通安装
         *
         * @author maple
         * @time 2018/12/13 10:13
         */
        public static void installApk(Context context, File file) {
            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());
        }
    
        /**
         * 静默安装
         * 需要root权限
         *
         * @author maple
         * @time 2018/12/13 10:13
         */
        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();
    
                //启动第三方应用重启app
                execLinuxCommand(context);
    
                //执行命令 - app kill
                process.waitFor();
    
                installApk(context, apk);
    
                //获取返回结果
                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();
                Log.e(TAG, "============= installSlient: Exception" + e.toString());
    
            } 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, "============= installSlient: finally" + e.toString());
                }
            }
    
            Log.e(TAG, "=================== successMsg: " + successMsg.toString() + ", errorMsg: " + errorMsg.toString());
    
            if ("Success".equals(successMsg.toString())) {
    
                Log.e(TAG, "======= apk install success");
    
            }
    
            installApk(context, apk);
        }
    
        /**
         * 重启app
         *
         * @author maple
         * @time 2018/12/13 10:13
         */
        private static void execLinuxCommand(Context context) {
            Log.e(TAG, "========== RTC");
    
            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 >= 23) {// 6.0及以上
                Log.e(TAG, "=================== android 6.0");
                mgr.setExactAndAllowWhileIdle(AlarmManager.RTC, System.currentTimeMillis() + 20000, restartIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4及以上
    
                Log.e(TAG, "=================== android 4.4.0");
                mgr.setExact(AlarmManager.RTC, System.currentTimeMillis() + 20000, restartIntent);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Android 静默安装

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