这两天一位朋友请教我如果给app默认授权呢?废话少说,我看了下代码给出两个方案:
1.修改PackageInstaller的源码(packages/apps/PackageInstaller/,如果你们用GMS包里面的,那就没有源码了,此方案不适合)
解释一下,不弹那个手动授权的dialog,直接获取权限就可以了,类似于执行dialog里面的接受的意思.
diff --git a/packages/apps/PackageInstaller/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java b/packages/apps/PackageInstaller/src/com/andro
index 56b3f46..21cfdca 100644
--- a/packages/apps/PackageInstaller/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java
+++ b/packages/apps/PackageInstaller/src/com/android/packageinstaller/permission/ui/GrantPermissionsActivity.java
@@ -157,12 +157,12 @@ public class GrantPermissionsActivity extends OverlayTouchActivity
}
}
- setContentView(mViewHandler.createView());
+ /*setContentView(mViewHandler.createView());
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
mViewHandler.updateWindowAttributes(layoutParams);
- window.setAttributes(layoutParams);
+ window.setAttributes(layoutParams);*/
if (!showNextPermissionGroupGrantRequest()) {
setResultAndFinish();
@@ -182,13 +182,13 @@ public class GrantPermissionsActivity extends OverlayTouchActivity
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
- mViewHandler.saveInstanceState(outState);
+ //mViewHandler.saveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
- mViewHandler.loadInstanceState(savedInstanceState);
+ //mViewHandler.loadInstanceState(savedInstanceState);
}
private boolean showNextPermissionGroupGrantRequest() {
@@ -221,10 +221,10 @@ public class GrantPermissionsActivity extends OverlayTouchActivity
resources = Resources.getSystem();
}
int icon = groupState.mGroup.getIconResId();
-
- mViewHandler.updateUi(groupState.mGroup.getName(), groupCount, currentIndex,
+ onPermissionGrantResult(groupState.mGroup.getName(), true, false);
+ /*mViewHandler.updateUi(groupState.mGroup.getName(), groupCount, currentIndex,
Icon.createWithResource(resources, icon), message,
- groupState.mGroup.isUserSet());
+ groupState.mGroup.isUserSet());*/
return true;
}
2.预制app的策略到手机里面
参考frameworks/base/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java文件,系统在开机初始化时候会去读取default-permissions这个文件,这个文件里面就是存放的每个app的预授权的信息,稍后再看这个源文件,我们先简单看下这块的代码.
这个java文件会去/system/etc 或者 /vendor/etc/下面读取这个文件,然后解析,最后授权权限
然后我们再看看这个文件default-permissions.xml里面存放的内容,相信不用我解释应该就知道了吧,大家也可以根据自己的需求定制
3.其它情况
上面两种情况是针对源码开发才可以操作,如果基于sdk开发,只能选择sdk的api小于android 6.0的,但是现在都到O了,很难绕过去,那也就只有显示了