Android軟件靜默安裝升級並自啓動

最近的需求,沒有界面顯示的一個Android系統開發板,要完成在線升級,所以必須要靜默升級(也就是不需要用戶看到安裝引導,不需要點擊任何東西)升級完成之後,自啓動升級後的軟件,下面和大家分享一下。有不正確的地方,還望大神指教。<_>

  1. 做到靜默安裝,升級,必須要有root權限
import android.content.Context;
import android.content.Intent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SilentInstall {

    /**
     * 靜默安裝升級軟件
     * @param context
     * @param apkPath  apk的路徑
     */
    public void runShellCommand(Context context, String apkPath) {
        Process process = null;
        BufferedReader bufferedReader = null;
        StringBuilder mShellCommandSB = new StringBuilder();
        mShellCommandSB.delete(0, mShellCommandSB.length());
        String[] cmd = new String[]{"/system/bin/sh", "-c", "pm install -r "+apkPath}; //調用bin文件,通過pm命令安裝軟件
        try {
            byte b[] = new byte[1024];
            process = Runtime.getRuntime().exec(cmd);
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line;

            while (bufferedReader.ready() && (line = bufferedReader.readLine()) != null) {
                mShellCommandSB.append(line);
            }
            Log.d("runShellCommand result : " + mShellCommandSB.toString());
            //安裝成功後的操作
            //靜態註冊自啓動廣播
            Intent intent = new Intent();
            //與清單文件的receiver的anction對應
            intent.setAction("android.intent.action.PACKAGE_REPLACED");
            //發送廣播
            context.sendBroadcast(intent);
            process.waitFor();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO: handle exception
                }
            }

            if (process != null) {
                process.destroy();
            }
        }
    }
}
  1. 然後就是編寫廣播類
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


public class UpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")){
            Toast.makeText(context,"升級了一個安裝包,重新啓動此程序", Toast.LENGTH_SHORT).show();
            Intent intent2 = new Intent(context, MainActivity.class);
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent2);
        }
        //接收安裝廣播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            String packageName = intent.getDataString();
            System.out.println("安裝了:" +packageName + "包名的程序");
        }
        //接收卸載廣播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            String packageName = intent.getDataString();
            System.out.println("卸載了:"  + packageName + "包名的程序");

        }
    }
}

AndroidManifest.xml註冊廣播

<receiver
    android:name=".receivers.UpdateReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED" />
        <action android:name="android.intent.action.PACKAGE_ADDED" />
        <action android:name="android.intent.action.PACKAGE_REMOVED" />
        <data android:scheme="package" />
    </intent-filter>
 </receiver>
  1. AndroidManifest.xml聲明權限
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission
        android:name="android.permission.INSTALL_PACKAGES"
        tools:ignore="ProtectedPermissions" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:sharedUserId="android.uid.system">
  1. 大功告成

參考文章:
https://blog.csdn.net/u012824494/article/details/61205836/ 不可實現的Android靜默安裝
https://blog.csdn.net/guolin_blog/article/details/47803149 Android靜默安裝實現方案,仿360手機助手秒裝和智能安裝功能

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章