最近项目中内嵌了谷歌框架,但是每次升级后都会弹窗申请高精度定位和定位权限申请等弹窗,如下图所示;需求是要去掉此弹窗。
- 先确认弹窗的应用,执行指令获取当前窗口应用 dumpsys window | grep mCurrentFocus
- 高精度弹窗
mCurrentFocus=Window{90ef9e7 u0 com.google.android.gms/com.google.android.location.network.ConfirmAlertActivity}
//com.google.android.gms 应用的 com.google.android.location.network.ConfirmAlertActivity
//GMS
- 定位权限弹窗
mCurrentFocus=Window{f5d1317 u0 com.google.android.gsf/com.google.android.gsf.settings.ConfirmLgaaylActivity}
//com.google.android.gsf 应用的 com.google.android.gsf.settings.ConfirmLgaaylActivity
//GoogleServicesFramework
-
由于第三方的APP内部弹窗,不能直接修改代码逻辑;因为是权限相关的弹窗,尝试直接默认授予相关权限,修改DefaultPermissionGrantPolicy.java,直接默认授予定位权限
路径: frameworks\base\services\core\java\com\android\server\pm\permission\DefaultPermissionGrantPolicy.java
private void grantDefaultSystemHandlerPermissions(PackageManagerWrapper pm, int userId) {
...
//GMS
if(pm.getPackageInfo("com.google.android.gms")!=null){
grantPermissionsToSystemPackage(pm, "com.google.android.gms", userId,
ALWAYS_LOCATION_PERMISSIONS);
}
//GoogleServicesFramework
if(pm.getPackageInfo("com.google.android.gsf")!=null){
grantPermissionsToSystemPackage(pm, "com.google.android.gsf", userId,
ALWAYS_LOCATION_PERMISSIONS);
}
//Carrier Services
if(pm.getPackageInfo("com.google.android.ims")!=null){
grantPermissionsToSystemPackage(pm, "com.google.android.ims", userId,
ALWAYS_LOCATION_PERMISSIONS);
}
//Google Play
if(pm.getPackageInfo("com.android.vending")!=null){
grantPermissionsToSystemPackage(pm, "com.android.vending", userId,
ALWAYS_LOCATION_PERMISSIONS);
}
...
}
-
编译后重新测试,发现还是会弹弹窗;没有办法,只能反编译APP查看具体的弹窗逻辑。这里用的反编译工具
jadx
如下图所示,代码中是通过 network_location_opt_in 属性判断是否跳转到ConfirmAlertActivity 弹窗。
uri值为
public static final Uri a = Uri.parse("content://com.google.settings/partner");
如下图所示,代码是通过use_location_for_services属性判断是否弹窗弹窗。
其uri值为
public static final Uri CONTENT_URI = Uri.parse("content://com.google.settings/partner");
因为弹窗都是通过属性 network_location_opt_in use_location_for_services 判断,而且属性都是ContentResolver值,uri已知,可以在开机后设置属性来实现屏蔽弹窗。具体代码实现可以在Launcher启动的界面的OnCreate执行,也可以在监听开机广播实现(开机广播有可能滞后),具体实现如下。
private void initGoogleGMSSetting() {
if (checkInstalled("com.google.android.gms")) {
try {
ContentResolver localContentResolver = mContext.getContentResolver();
ContentValues localContentValues = new ContentValues();
localContentValues.put("name", "network_location_opt_in");
localContentValues.put("value", 1);
localContentResolver.insert(Uri.parse("content://com.google.settings/partner"), localContentValues);
} catch (IllegalArgumentException e) {
Log.e("GoogleSettings", "Can't set key " + "network_location_opt_in" + " in " + "content://com.google.settings/partner", e);
}
}
if (checkInstalled("com.google.android.gsf")) {
try {
ContentResolver localContentResolver = mContext.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "use_location_for_services");
values.put("value", 1 + "");
localContentResolver.insert(Uri.parse("content://com.google.settings/partner"), values);
} catch (IllegalArgumentException e) {
Log.e("GoogleSettings", "Can't set key " + "use_location_for_services" + " in " + "content://com.google.settings/partner", e);
}
}
}
AndroidManifest.xml 需要添加 相关权限
<uses-permission android:name="com.google.android.providers.settings.permission.WRITE_GSETTINGS" />
此步骤执行禁止了弹窗弹窗,最后还需默认授予定位权限,具体修改如上面修改。
此方式是有效禁止了弹窗,但是不确定会不会影响Google框架使用需要后续测试发现,如果有更好的方式请多指教。
参考
https://blog.csdn.net/wxd_csdn_2016/article/details/106084389/