Android之限制安裝apk的個數
瞭解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);
}
}