本文主要是介绍Android高通 12/13静默安装和卸载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、静默安装和静默卸载
涉及代码路径感兴趣可以去看下如下所示,这里不作重点赘述哈
Package Manger
frameworks/base/services/java/com/android/server/pm/Settings.java
frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
frameworks/base/services/java/com/android/server/pm/IPackageManager.aidl
frameworks/base/services/java/com/android/server/pm/PackageSignatures.java
frameworks/base/services/java/com/android/server/pm/PreferredActivity.java
frameworks/services/java/com/android/server/PreferredComponent.java
frameworks/core/java/android/content/IntentFilter.java
frameworks/base/core/java/android/content/pm/PackageParser.java
frameworks/base/services/java/com/android/server/pm/Installer.java
frameworks/base/core/java/com/android/internal/app/IMediaContainerService.aidl
frameworks/base/packages/DefaultContainerService/src/com/android/defcontainer/DefaultContainerService.javaPackage Installer
packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java
packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageUtil.java
packages/apps/PackageInstaller/src/com/android/packageinstaller/InstallAppProgress.java
2、静默安装apk 代码
/*** 静默安装应用** @param path* @return*/public boolean installApp(String path) {PackageManager packageManager = mContext.getPackageManager();PackageInfo info = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);ApplicationInfo appInfo = null;String pkgName = "";if (info != null) {appInfo = info.applicationInfo;pkgName = appInfo.packageName;}if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {try {PackageInstaller packageInstaller = mContext.getPackageManager().getPackageInstaller();PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);if (!pkgName.equals("")) {params.setAppPackageName(pkgName);Method allowDowngrade = PackageInstaller.SessionParams.class.getMethod("setAllowDowngrade", boolean.class);allowDowngrade.setAccessible(true);allowDowngrade.invoke(params, true);int sessionId = packageInstaller.createSession(params);PackageInstaller.Session session = packageInstaller.openSession(sessionId);OutputStream os = session.openWrite(pkgName, 0, -1);InputStream is = new FileInputStream(path);byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer)) != -1) {os.write(buffer, 0, len);}session.fsync(os);os.close();is.close();session.commit(PendingIntent.getBroadcast(mContext, sessionId,new Intent(Intent.ACTION_MAIN), FLAG_IMMUTABLE).getIntentSender());}} catch (Exception e) {e.printStackTrace();}}return true;}
3、静默卸载 apk代码
/*** 静默卸载应用** @param path* @return*/public boolean uninstallApp(String path) {class InstallResultReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {}}Intent broadcastIntent = new Intent(mContext, InstallResultReceiver.class);PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 1,broadcastIntent, FLAG_IMMUTABLE);PackageInstaller packageInstaller = mContext.getPackageManager().getPackageInstaller();packageInstaller.uninstall(path, pendingIntent.getIntentSender());return true;}
4、记得权限问题一般data/data system/priv-app system/app等路径
5、apk调用代码示例
/*** 静默安装(data/data 系统应用不能放在sdcard目录安装 会冲突 system/app or /system/priv-app)*/installApp("data/data/xxx.apk");/*** 静默卸载*/uninstallApp("data/data/xxx.apk");
转载请注明出处,谢谢!
参考文章:
Android 包管理机制---PackageInstaller - 简书
这篇关于Android高通 12/13静默安装和卸载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!