private static void clientInstall(final Context context) {
new Thread(new Runnable() {
public void run() {
copyApkToSD(context);
File file = new File(APP_PATH);
if(!file.exists())
return;
Uri uri = Uri.fromFile(file);
PackageManager pm = context.getPackageManager();
int installFlags = 0;
installFlags |= INSTALL_REPLACE_EXISTING;// 0x00000002
MyPackageInstallObserver observer = new MyPackageInstallObserver();
try {
Method installPackage = PackageManager.class.getMethod(
"installPackage", Uri.class,
IPackageInstallObserver.class, int.class,
String.class);
installPackage.invoke(pm, uri, observer, installFlags,
GARBAGECLEAN_PACKAGE_NAME);
} catch (Exception e) {
e.printStackTrace();
}
}
}).run();
}
private static class MyPackageInstallObserver extends IPackageInstallObserver.Stub{
@Override
public void packageInstalled(String packageName, int returnCode)
throws RemoteException {
if(returnCode == 1){
NBLog.i(TAG, "replace success");
}else{
NBLog.i(TAG, "replace fail , and returnCode is : " + returnCode);
}
File file = new File(APP_PATH);
if(file.exists()){
if(file.delete()){
NBLog.i(TAG, "delete apk success");
}else{
NBLog.i(TAG, "delete apk fail");
}
}
}
}静默安装
最新推荐文章于 2023-03-30 15:32:43 发布
本文介绍了一种使用自定义观察者的方式,在Android设备上静默安装APK,并能够检查是否存在旧版本APK,若存在则进行替换。通过反射调用`PackageManager`的`installPackage`方法实现。
2747

被折叠的 条评论
为什么被折叠?



