Android 静默更新

最近在做智慧屏项目,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();
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值