安卓静默安装或升级后,apk不自动解决方案,Android 8.1
由于做的产品是pos机,不是一个普通的app,所以需要使用静默升级的工能
1,静默升级代码:
public static String silentInstallApkByReflect(String apkPath, Context context) {
String result = "";
try {
PackageManager packageManager = context.getPackageManager();
Class<?> pmClz = packageManager.getClass();
if (Build.VERSION.SDK_INT >= 21) {
Class<?> aClass = Class.forName("android.app.PackageInstallObserver");
Constructor<?> constructor = aClass.getDeclaredConstructor();
constructor.setAccessible(true);
Object installObserver = constructor.newInstance();
Method method = pmClz.getDeclaredMethod("installPackage", Uri.class, aClass, int.class, String.class);
method.setAccessible(true);
method.invoke(packageManager, Uri.fromFile(new File(apkPath)), installObserver, 2, null);
} else {
Method method = pmClz.getDeclaredMethod("installPackage", Uri.class, Class.forName("android.content.pm.IPackageInstallObserver"), int.class, String.class);
method.setAccessible(true);
method.invoke(packageManager, Uri.fromFile(new File(apkPath)), null, 2, null);
}
result = "success";
} catch (Exception e) {
e.printStackTrace();
result = e.toString();
}
return result;
}
第一个参数是要安装apk的路径,第二参数是全局Context
安装成功后不会自动重启,所以需要用一个第三方的软件将app拉起来
我是在Lancher中做这样一个操作的
我们需要一个广播接收器,来监听安装后的广播
public class UpdateReceiver extends BroadcastReceiver {
private static final String TAG = UpdateReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
String packageStr = intent.getDataString();
Log.e(TAG, "onReceive:升级了一个安装包,重新启动此程序 packageStr:" + packageStr + " action:" + intent.getAction());
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {//接收升级广播
String packageName = packageStr.substring(packageStr.lastIndexOf(":") + 1);
if (packageName.startsWith("com.bsit")) {
restartAPP(context, packageName);//升级完自身app,重启自身
}
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {//接收安装广播
String packageName = packageStr.substring(packageStr.lastIndexOf(":") + 1);
if (packageName.startsWith("com.bsit")) {
/*SystemUtil.reBootDevice();*/
// restartAPP(context, packageName);//升级完自身app,重启自身
}
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) { //接收卸载广播
Log.e(TAG, "onReceive:卸载了" + packageStr);
}
}
/**
* 重启整个APP
*
* @param context
*/
public static void restartAPP(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
Log.e(TAG, "获取启动intent:" + intent);
if (intent != null) {
context.startActivity(intent);
}
}
}
AndroidManifest做如下声明:
<receiver
android:name=".UpdateReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
这样安装完后就可以重启我的app了
由于安装完成后总会受到 remove add replace 这三种广播,所以我只在replace后重启我的app就可以了
本文地址:https://blog.csdn.net/wxj280306451/article/details/108979805