安裝後自啓動很久之前的問題了 今天剛好遇到 記錄一下:添加兩行命令就搞定了
一般的安裝
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大小來判斷) 這裏應該還有其他的解決方案
以上就是靜默安裝後重啓的方案了!如果還有其他的方法的 歡迎分享!!!!!!!!!!!!!!!!!
有什麼問題可以留言!會在最短的時間內答覆