Android允许应用具有安装权限

     本文只针对调用PackageInstaller安装流程,ROM中有时需要集成一些第三方的应用商城或着launcher,但需要此应用可能具有安装权限,像大部分机顶盒设备,投影设备禁止安装未知来源的控制对用户是不可见的,功能很简单直接贴代码;

     PackageInstallerActivity中有个checkIfAllowedAndInitiateInstall()函数来判断是否允许安装,允许的话会调用initiateInstall()弹出安装应用的界面,禁止安装未知来源的应用时会弹出禁止安装未知来源的dialog;

     PackageInstallerActivity中有个变量mOriginatingPackage,此变量就是调用安装应用事件的应用的包名,只需在initiateInstall()的if判断中加入自己的判断逻辑即可。

/**
*检查是否允许安装包,如果允许,则启动安装。如果不允许
*显示相应的对话框。
*/
    private void checkIfAllowedAndInitiateInstall() {
        // Check for install apps user restriction first.
        final int installAppsRestrictionSource = mUserManager.getUserRestrictionSource(
                UserManager.DISALLOW_INSTALL_APPS, Process.myUserHandle());
        if ((installAppsRestrictionSource & UserManager.RESTRICTION_SOURCE_SYSTEM) != 0) {
            showDialogInner(DLG_INSTALL_APPS_RESTRICTED_FOR_USER);
            return;
        } else if (installAppsRestrictionSource != UserManager.RESTRICTION_NOT_SET) {
            startActivity(new Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS));
            finish();
            return;
        }
        //看这里的判断条件1.允许安装未知来源应用 2.非未知来源应用发起的安装事件
        //3.这是我加的逻辑
        if (mAllowUnknownSources || !isInstallRequestFromUnknownSource(getIntent()) || new StmConfig().isUserCustomAllowInstallPkg(mOriginatingPackage)) {
            initiateInstall();
        } else {
            // Check for unknown sources restriction
            final int unknownSourcesRestrictionSource = mUserManager.getUserRestrictionSource(
                    UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, Process.myUserHandle());
            if ((unknownSourcesRestrictionSource & UserManager.RESTRICTION_SOURCE_SYSTEM) != 0) {
                showDialogInner(DLG_UNKNOWN_SOURCES_RESTRICTED_FOR_USER);
            } else if (unknownSourcesRestrictionSource != UserManager.RESTRICTION_NOT_SET) {
                startActivity(new Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS));
                finish();
            } else {
                handleUnknownSources();
            }
        }
    }

 

你可能感兴趣的:(Android开发,安装应用,未知来源,Package,Installer)