android实现静默更新安装并自动启动
2017-12-22 16:53
274 查看
因为做的项目是装在固定设备上的,所有设备都已root
一:当前版本号与服务器版本对比 如有更新则去服务器下载新版本
这个网上很多教程不多说
二:下载完保存到某目录下,自动安装,无需手动点击确认
实现实际使用的是su pm install -r filepath命令。
核心代码如下:
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();
}
}
}
三、自启动
注册广播UpdateRestartReceiver,监听是否重新装了安装包
public class UpdateRestartReceiver 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, SplashActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
}
AndroidManifest.xml中
<receiver android:name=".updateversion.UpdateRestartReceiver"
>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
静态注册启用广播:
//静态注册自启动广播
Intent intent=new Intent();
//与清单文件的receiver的anction对应
intent.setAction("android.intent.action.PACKAGE_REPLACED");
//发送广播
sendBroadcast(intent);
所需权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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" />
一:当前版本号与服务器版本对比 如有更新则去服务器下载新版本
这个网上很多教程不多说
二:下载完保存到某目录下,自动安装,无需手动点击确认
实现实际使用的是su pm install -r filepath命令。
核心代码如下:
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();
}
}
}
三、自启动
注册广播UpdateRestartReceiver,监听是否重新装了安装包
public class UpdateRestartReceiver 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, SplashActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
}
AndroidManifest.xml中
<receiver android:name=".updateversion.UpdateRestartReceiver"
>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
静态注册启用广播:
//静态注册自启动广播
Intent intent=new Intent();
//与清单文件的receiver的anction对应
intent.setAction("android.intent.action.PACKAGE_REPLACED");
//发送广播
sendBroadcast(intent);
所需权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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" />
相关文章推荐
- android 4.0.4系统下实现apk的静默安装和启动
- android 4.0.4系统下实现apk的静默安装和启动
- android 4.0.4系统下实现apk的静默安装和启动
- Android应用自动更新功能的实现!!!软件更新,自动下载,安装
- android 版本更新 静默安装及自启动
- android 4.0.4系统下实现apk的静默安装和启动
- 转载 :Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- android 自动更新 覆盖安装后 自动启动的问题
- Android自动化技术之自动monkey:shell脚本实现自动创建模拟器、启动模拟器、自动安装apk并进行monkey测试
- Android自动批量静默安装应用脚本实现
- Android 4.0.4系统下实现apk的静默安装和启动
- Android 4.0.4系统下实现apk的静默安装和启动
- unity3d开发android小游戏版本版本更新后自动安装功能的实现
- 安卓自动更新,静默更新,替换友盟更新可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新
- Android 一步一步实现版本自动更新(第二步 下载和安装apk)
- Android实现版本更新和自动安装
- ***android 4.0.4系统下实现apk的静默安装和启动
- Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
- [Android实例] android应用静默升级,安装完成后自动启动该应用
- Android客户端apk自动检测更新自动下载自动安装的实现方法