android 应用自动升级安装并打开新版本应用
来源:互联网 发布:mac有安卓模拟器吗 编辑:程序博客网 时间:2024/05/30 23:04
在一些特殊用途的时候,我们需要为APP设计自动安装升级包,并能在自动静默安装后自动打开应用。一般这种用途大都在android工控机上,因为这样的机器大都是具备root权限的,不然的话,如果没有root权限,自动静默安装及自动打开都是不能实现的。
首先,我写了一个工具类用于处理静默安装:
public class UpdateUtil {public static boolean install(String path, Context context) {// 判断root权限if (isRoot()) {// 有root权限,静默安装return apkInstall(path);} else {// 无root权限,用意图安装,无法自动,没有意义File file = new File(path);if (!file.exists())return false;Intent intent = new Intent();intent.setAction("android.intent.action.VIEW");intent.addCategory("android.intent.category.DEFAULT");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");context.startActivity(intent);return true;}}/** 静默安装 */private static boolean apkInstall(String path) {PrintWriter PrintWriter = null;Process process = null;try {process = Runtime.getRuntime().exec("su");PrintWriter = new PrintWriter(process.getOutputStream());PrintWriter.println("chmod 777 " + path);PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");PrintWriter.println("pm install -r " + path);PrintWriter.flush();PrintWriter.close();int value = process.waitFor();return value == 0;} catch (Exception e) {e.printStackTrace();} finally {if (process != null) {process.destroy();}}return false;}/** 判断当前设备是否已经获取到root权限 */private static boolean isRoot() {PrintWriter PrintWriter = null;Process process = null;try {process = Runtime.getRuntime().exec("su");PrintWriter = new PrintWriter(process.getOutputStream());PrintWriter.flush();PrintWriter.close();int value = process.waitFor();return value == 0;} catch (Exception e) {e.printStackTrace();} finally {if (process != null) {process.destroy();}}return false;}}另外需要在新版本中,注册广播,在androidManifest.xml中添加权限并注册:
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.updatetest.utils.TestReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <data android:scheme="package" /> </intent-filter> </receiver>
TestReceiver中注意处理<pre name="code" class="html">android.intent.action.PACKAGE_REPLACED
从而实现自动启动该应用
if (intent.getAction().equalsIgnoreCase("android.intent.action.PACKAGE_REPLACED")) {Intent intent2 = new Intent(context, MainActivity.class);intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent2); }
0 0
- android 应用自动升级安装并打开新版本应用
- android实现自动升级并安装打开
- android实现自动升级并安装打开
- android实现自动升级并安装打开
- 安卓新版本升级,并自动安装apk
- Android 判断应用是否安装 并打开或下载应用
- 安装新版本应用
- android应用客户端自动升级
- android应用客户端自动升级
- android应用客户端自动升级
- android应用客户端自动升级 .
- android自动升级应用实现
- android安装本地应用,并提示打开还是完成安装
- Android应用检测并下载更新新版本的方法
- [Android实例] android应用静默升级,安装完成后自动启动该应用
- [Android实例] android 应用客户端自动升级
- Android实现应用下载并自动安装apk包
- Android实现应用下载并自动安装apk包
- 3333333333333
- JDBC处理器和BeanShell取样器的组合应用
- Python Web 9 —— flask中在js中如何解析render_template传递给html的数据
- iOS App上架流程
- Error:2002:Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
- android 应用自动升级安装并打开新版本应用
- java SSM 框架 多数据源 代码生成器
- 关于 iOS自定义转场动画
- String 转 java.sql.Timestamp 异常详解
- Linux学习目录
- 【BLE-CC2640】CC2640之按键
- Android的位深
- 点击控件 让ListView或者ScrollView 返回到顶部
- 下载的谷歌卫星地图如何发布地图服务的两种方法