现在很多app为了更好的用户体验纷纷开始使用静默安装,这段时间公司也刚好有一个这样的新项目,是电视盒子的tv项目,系统是定制的,可以使用系统签名,所以我们这里的内容只是有系统签名权限的app的静默安装和安装后自己启动自己。下面是测试通过的方案实现代码:
附:系统签名打包方法
- 工具类:
public class ApkController {
/**
* 描述: 安装
*/
public static boolean install(String apkPath,Context context){
// 先判断手机是否有root权限
if(hasRootPerssion()){
Logc.d("root权限获取成功");
// 有root权限,利用静默安装实现
return clientInstall(apkPath);
}
else{
Logc.d("没有root权限");
// 没有root权限,利用意图进行安装
File file = new File(apkPath);
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;
}
}
/**
* 描述: 卸载
*/
public static boolean uninstall(String packageName,Context context){
if(hasRootPerssion()){
// 有root权限,利用静默卸载实现
return clientUninstall(packageName);
}else{
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
return true;
}
}
/**
* 判断手机是否有root权限
*/
public static boolean hasRootPerssion(){
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 returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默安装
*/
public static boolean clientInstall(String apkPath){
Logc.e("apkPath:"+apkPath);
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r "+apkPath);//-r 重新安装应用,保留应用数据
// PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默卸载
*/
public static boolean clientUninstall(String packageName){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
private static boolean returnResult(int value){
// 代表成功
if (value == 0) {
return true;
} else if (value == 1) { // 失败
return false;
} else { // 未知情况
return false;
}
}
}
-
创建广播接收器
/** * 我们通过广播来启动Activity的时候如果不设置intent的FLAG_ACTIVITY_NEW_TASK属性,就会报这个异常: * android.util.AndroidRuntimeException: * Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. * 就是说在activity上下文之外调用startActivity需要FLAG_ACTIVITY_NEW_TASK属性。 * @author Administrator */ public class InstallReceiver 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(); Logc.d("静默启动成功"); Intent intent2 = new Intent(context, WelcomeActivity.class); intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent2); } } }
-
还要在清单文件中注册广播:
<category android:name="android.intent.category.HOME" /> </intent-filter>
使用:
service中新开线程或者使用IntentService调用ApkController.install()即可。
几个注意点:
- 前提是旧版本要有系统签名才能有系统权限。
- 不需要root,因为root后在安装时申请su权限会权限提示框。
- apk放到system/app下面新建app相应目录,比如我的app名字为Qing.apk,则新建Qing文件夹,即最终路径为system/app/Qing/Qing.apk,为什么要建这个文件夹呢,其实查看app下面其他系统app可以看出都有一个以Apk名字命名的文件夹,首字母大写,系统重启时会检测app下的apk是否已经安装,如果未安装则自动安装,生成一个同样的文件夹。
- android静默安装apk使用android.content.pm.PackageManager.installPackage(Uri packageURI, IPackageInstallObserver observer, int flags, String installerPackageName)进行安装应用程序,ovserver 和packagename都可为null,但是为系统级应用静默升级时,由于在android源代码里面的PackageManager会检查versionCode:
如果更新或者升级后系统内置应用,遇到重启Android系统后内置应用被还原,那是因为手动安装的APK版本号和系统内置API版本号一样。
1、Android系统应用更新机制
系统为每个应用在AndroidMainfest.xml提供了versionName、versionCode两个属性。
versionName:String类型,用来给应用的使用者来查看版本.
versionCode:Integer类型,作为系统判断应用是否能升级的依据。2、Android系统内置应用更新判断代码
代码来自frameworks/base/services/java/com/android/server/PackageManagerService.java
中 scanPackageLI函数的package更新判断条件(约第2580-2621行附近)// First check if this is a system package that may involve an update
if (updatedPkg != null && (parseFlags&PackageParser.PARSE_IS_SYSTEM)
!= 0) {
if (!ps.codePath.equals(scanFile)) {
// The path has changed from what was last scanned… check the
// version of the new path against what we have stored to determine
// what to do.
if (pkg.mVersionCode < ps.versionCode) {
// The system package has been updated and the code path does not match
// Ignore entry. Skip it.从上面代码注释可以知道:更新系统内置应用时,如果新的versionCode没有大于当前安装的版本,更新将被忽略。
参考:https://blog.csdn.net/qq_29586601/article/details/79935425
https://www.jianshu.com/p/e1005082e365