版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZhangYu971014/article/details/86129324
最近的需求,没有界面显示的一个Android系统开发板,要完成在线升级,所以必须要静默升级(也就是不需要用户看到安装引导,不需要点击任何东西)升级完成之后,自启动升级后的软件,下面和大家分享一下。有不正确的地方,还望大神指教。<_>
- 做到静默安装,升级,必须要有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();
}
}
}
}
- 然后就是编写广播类
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>
- 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">
- 大功告成