android 9.0跳过“未知来源”进行安装应用
需求:點擊更新應用進行安裝的過程中彈出“未知來源”設置提示,需要跳過該步驟直接安裝。
方案1?
分析:
1.首先9.0 app安裝是通過packages/apps/PackageInstaller里面的應用進行安裝的。
2.其次定位到安裝界面是/src/com/android/packageinstaller/PackageInstallerActivity.java
3.查看handleUnknownSources里面進行了代碼控制,至此問題已解決(或者直接通過appOpMode和包名進行判斷調用initiateInstall()函數也行,不建議),但是我們還得深入看下彈框做了什么動作。。。
private void handleUnknownSources() {if (mOriginatingPackage == null) {Log.i(TAG, "No source found for package " + mPkgInfo.packageName);showDialogInner(DLG_ANONYMOUS_SOURCE);return;}Log.e(TAG, "wgd==No source found for package " + mPkgInfo.packageName);// Shouldn't use static constant directly, see b/65534401.//提前設置允許該應用獲取未知來源權限if("com.ctg.itrdc.clouddesk".equals(mPkgInfo.packageName)){mAppOpsManager.setMode(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES, mOriginatingUid,mPkgInfo.packageName, AppOpsManager.MODE_ALLOWED);}final int appOpCode =AppOpsManager.permissionToOpCode(Manifest.permission.REQUEST_INSTALL_PACKAGES);final int appOpMode = mAppOpsManager.noteOpNoThrow(appOpCode,mOriginatingUid, mOriginatingPackage);//根據appOpMode判斷是否已獲得未知來源安裝權限 MODE_ERRORED沒獲取 MODE_ALLOWED已獲取 switch (appOpMode) {case AppOpsManager.MODE_DEFAULT:mAppOpsManager.setMode(appOpCode, mOriginatingUid,mOriginatingPackage, AppOpsManager.MODE_ERRORED);// fall throughcase AppOpsManager.MODE_ERRORED:showDialogInner(DLG_EXTERNAL_SOURCE_BLOCKED);break;case AppOpsManager.MODE_ALLOWED:initiateInstall();break;default:Log.e(TAG, "Invalid app op mode " + appOpMode+ " for OP_REQUEST_INSTALL_PACKAGES found for uid " + mOriginatingUid);finish();break;}}
4.showDialogInner(DLG_EXTERNAL_SOURCE_BLOCKED)彈框做了什么工作呢??接下來調用了創建窗口函數createDialog()
/*** Replace any dialog shown by the dialog with the one for the given {@link #createDialog id}.** @param id The dialog type to add*/private void showDialogInner(int id) {DialogFragment currentDialog =(DialogFragment) getFragmentManager().findFragmentByTag("dialog");if (currentDialog != null) {currentDialog.dismissAllowingStateLoss();}DialogFragment newDialog = createDialog(id);if (newDialog != null) {newDialog.showAllowingStateLoss(getFragmentManager(), "dialog");}}
/*** Create a new dialog.** @param id The id of the dialog (determines dialog type)** @return The dialog*/private DialogFragment createDialog(int id) {switch (id) {case DLG_PACKAGE_ERROR:return SimpleErrorDialog.newInstance(R.string.Parse_error_dlg_text);case DLG_OUT_OF_SPACE:return OutOfSpaceDialog.newInstance(mPm.getApplicationLabel(mPkgInfo.applicationInfo));case DLG_INSTALL_ERROR:return InstallErrorDialog.newInstance(mPm.getApplicationLabel(mPkgInfo.applicationInfo));case DLG_NOT_SUPPORTED_ON_WEAR:return NotSupportedOnWearDialog.newInstance();case DLG_INSTALL_APPS_RESTRICTED_FOR_USER:return SimpleErrorDialog.newInstance(R.string.install_apps_user_restriction_dlg_text);case DLG_UNKNOWN_SOURCES_RESTRICTED_FOR_USER:return SimpleErrorDialog.newInstance(R.string.unknown_apps_user_restriction_dlg_text);case DLG_EXTERNAL_SOURCE_BLOCKED://未知來源return ExternalSourcesBlockedDialog.newInstance(mOriginatingPackage);case DLG_ANONYMOUS_SOURCE:return AnonymousSourceDialog.newInstance();}return null;}
/*** An error dialog shown when external sources are not allowed*/public static class ExternalSourcesBlockedDialog extends AppErrorDialog {static AppErrorDialog newInstance(@NonNull String originationPkg) {ExternalSourcesBlockedDialog dialog = new ExternalSourcesBlockedDialog();dialog.setArgument(originationPkg);return dialog;}@Overrideprotected Dialog createDialog(@NonNull CharSequence argument) {try {PackageManager pm = getActivity().getPackageManager();ApplicationInfo sourceInfo = pm.getApplicationInfo(argument.toString(), 0);return new AlertDialog.Builder(getActivity()).setTitle(pm.getApplicationLabel(sourceInfo)).setIcon(pm.getApplicationIcon(sourceInfo)).setMessage(R.string.untrusted_external_source_warning).setPositiveButton(R.string.external_sources_settings,(dialog, which) -> {Intent settingsIntent = new Intent();settingsIntent.setAction(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);final Uri packageUri = Uri.parse("package:" + argument);settingsIntent.setData(packageUri);try {getActivity().startActivityForResult(settingsIntent,REQUEST_TRUST_EXTERNAL_SOURCE);} catch (ActivityNotFoundException exc) {Log.e(TAG, "Settings activity not found for action: "+ Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);}}).setNegativeButton(R.string.cancel,(dialog, which) -> getActivity().finish()).create();} catch (NameNotFoundException e) {Log.e(TAG, "Did not find app info for " + argument);getActivity().finish();return null;}}}
?Intent settingsIntent = new Intent();
?settingsIntent.setAction(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
?接下來主要看看是跳轉到哪了,這其實調用到設置apk里面了。通過界面文字搜索最終定位到
/src/com/android/settings/applications/appinfo/ExternalSourcesDetails.java類
?最終發現設置打開應用“未知來源”權限執行了以下代碼,至此得出第3步修改方案。
我們也可以按照下面進行初始化實現?
private void openAppInstallUnknownSource(Context context){try {String packageName = SystemProperties.get("persist.sys.app_install_unknown_source");if(!android.text.TextUtils.isEmpty(packageName)){AppOpsManager mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);PackageManager mPm = context.getPackageManager();PackageInfo mPackageInfo = mPm.getPackageInfo(packageName,PackageManager.MATCH_DISABLED_COMPONENTS |PackageManager.MATCH_ANY_USER |PackageManager.GET_SIGNATURES |PackageManager.GET_PERMISSIONS);mAppOpsManager.setMode(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES,mPackageInfo.applicationInfo.uid, packageName,AppOpsManager.MODE_ALLOWED);}}catch (Exception e) {Log.e(TAG, "Exception" + e);}}
修改方案2:
frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java
在private void grantDefaultSystemHandlerPermissions(int userId) 函數中添加如下代碼:
PackageParser.Package feedBackPackage = getPackage("net.sunniwell.dss");if (feedBackPackage != null&& doesPackageSupportRuntimePermissions(feedBackPackage)) {grantRuntimePermissions(feedBackPackage, STORAGE_PERMISSIONS, userId);}
2.判斷是否已獲取未知來源權限
//getPackageManager()表示當前應用是否獲得未知來源權限,一般用于自升級判斷
boolean canRequestPackageInstalls = getPackageManager().canRequestPackageInstalls();
log.d("upgradech:canRequestPackageInstalls = " + canRequestPackageInstalls);
總結
以上是生活随笔為你收集整理的android 9.0跳过“未知来源”进行安装应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis 5.0 正式版发布了,19
- 下一篇: 如何使用电脑监控软件管理员工公司电脑如何