最近在做智慧屏项目,apk需要静默更新
1、配置权限,因为静默升级需要系统权限所有我们在Manifest文件中加上
android:sharedUserId=“android.uid.system”
并且配置安装apk的权限
android:name=“android.permission.INSTALL_PACKAGES”
2、找厂家要到签名工具 signapk.jar platform.x509.pem platform.pk8
首先把项目打成一个未签名的包,然后在对这个包进行签名,找到签名文件所在位置,调用以下命 令进行签名
java -jar signapk.jar platform.x509.pem platform.pk8 app-debug.apk app-test-sign.apk
3、检测更新并下载的部分这里就省略了,直接上更新后静默安装代码
//执行安装
protected void excutesucmd(String currenttempfilepath) {
Process process = null;
OutputStream out = null;
InputStream in = null;
try {
// 请求root
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
// 调用安装
out.write(("pm install -r " + currenttempfilepath + "\n").getBytes());
in = process.getInputStream();
int len = 0;
byte[] bs = new byte[256];
while (-1 != (len = in.read(bs))) {
String state = new String(bs, 0, len);
if (state.equals("success\n")) {
//安装成功后的操作
//静态注册自启动广播
Intent intent=new Intent();
//与清单文件的receiver的anction对应
intent.setAction("android.intent.action.PACKAGE_REPLACED");
//发送广播
sendBroadcast(intent);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、静默安装后自启动
在AndroidMainifest.xml 中注册
<receiver android:name=".Receiver.UpdateStartReceiver”>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
public class UpdateStartReceiver extends BroadcastReceiver{
private final String ACTION_BOOT = "android.intent.action.PACKAGE_REPLACED";
/**
* 接收广播消息后都会进入 onReceive 方法,然后要做的就是对相应的消息做出相应的处理
*
* @param context 表示广播接收器所运行的上下文
* @param intent 表示广播接收器收到的Intent
*/
@Override
public void onReceive(Context context, Intent intent) {
/**
* 如果 系统 启动的消息,则启动 APP 主页活动
*/
if (ACTION_BOOT.equals(intent.getAction())) {
Intent intentMainActivity = new Intent(context, WelcomeActivity.class);
intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentMainActivity);
Toast.makeText(context, "更新完毕~", Toast.LENGTH_LONG).show();
}
}
}

6545

被折叠的 条评论
为什么被折叠?



