Android之限制安装apk的个数

    xiaoxiao2022-07-07  228

    了解apk安装流程的都知道apk的安装主要涉及的frameworks层下的PackageManagerService.java类的installPackageLI方法。

    找到,添加,本例以6个为例:

     try (PackageFreezer freezer = freezePackageForInstall(pkgName, installFlags,                 "installPackageLI")) {             if (replace) {                 if (pkg.applicationInfo.isStaticSharedLibrary()) {                     // Static libs have a synthetic package name containing the version                     // and cannot be updated as an update would get a new package name,                     // unless this is the exact same version code which is useful for                     // development.                     PackageParser.Package existingPkg = mPackages.get(pkg.packageName);                     if (existingPkg != null && existingPkg.mVersionCode != pkg.mVersionCode) {                         res.setError(INSTALL_FAILED_DUPLICATE_PACKAGE, "Packages declaring "                                 + "static-shared libs cannot be updated");                         return;                     }                 }                 replacePackageLIF(pkg, parseFlags, scanFlags | SCAN_REPLACING, args.user,                         installerPackageName, res, args.installReason);             } else {                 //Slog.d(TAG,"-----installNewPackage----");                 /*-------------------add start-------------------------------*/                 int dataAppSize = 0;                 int size = mPackages.size();                 for(int i = 0;i<mPackages.size();i++){                     String pkgNamei = mPackages.keyAt(i);                     //Slog.d(TAG,"----pkgNamei:"+pkgNamei);                     PackageSetting ps = mSettings.mPackages.get(pkgNamei);                     if (ps.pkg != null && ps.pkg.applicationInfo != null) {                         if((ps.pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){ //data                             dataAppSize = dataAppSize + 1;                             //Slog.d(TAG,"-----installNewPackage----dataAppSize:"+dataAppSize);                             if(dataAppSize >= 6){                                 //mContext.sendBroadcast(new Intent("com.action.installapp.limt"));                                 Toast.makeText(mContext, "The number of APP has reached the upper limit.", Toast.LENGTH_LONG).show();                                 res.setError(INSTALL_FAILED_INSTANT_APP_INVALID,                         "The number of APP has reached the upper limit.");                                 return;                             }                         }                     }                 }                 /*--------------------- add end-----------------------------*/                 installNewPackageLIF(pkg, parseFlags, scanFlags | SCAN_DELETE_DATA_ON_FAILURES,                         args.user, installerPackageName, volumeUuid, res, args.installReason);             }         }  

     

    参考:https://blog.csdn.net/xl19862005/article/details/52102295

    最新回复(0)