Android11 应用默认获取通知使用权限(可以获取系统所有通知信息)
在 Android 11 中,系统对应用的权限管理进行了进一步的细化和增强。关于默认给予应用通知权限这一特性,通常情况下,
应用在安装后并不会自动获得所有的权限,包括通知权限。应用必须明确请求所需的权限,并且用户需要在权限请求对话框中授权。
//检查是否具有通知使用权,都是在系统中的secure 数据库,利用命令adb shell settings list secure可以查看
C:\Users\xx>adb shell settings list secure可以查看
accessibility_display_inversion_enabled=null
accessibility_display_magnification_enabled=0
accessibility_display_magnification_scale=2.0
accessibility_enabled=0
adaptive_sleep=null
android_id=6bcce84ef38428a
autofill_service=
aware_enabled=0
aware_lock_enabled=0
backup_enabled=0
backup_transport=com.android.localtransport/.LocalTransport
bluetooth_name=rk3566
charging_sounds_enabled=0
charging_vibration_enabled=1
clock_seconds=null
default_input_method=com.android.inputmethod.latin/.LatinIME
double_tap_to_wake=1
doze_pulse_on_double_tap=null
doze_tap_gesture=null
enabled_input_methods=com.android.inputmethod.latin/.LatinIME
enabled_notification_assistant=android.ext.services/android.ext.services.notification.Assistant
enabled_notification_policy_access_packages=com.android.demo:com.android.camera2//这里
high_priority=null
....
方案一:
刷机第一次开机NotificationManagerService开启流程:
loadPolicyFile()->allowDefaultApprovedServices(USER_SYSTEM)
->allowDndPackage(...)
->getBinderService().setNotificationPolicyAccessGranted(packageName, true);
protected void allowDefaultApprovedServices(int userId) {
ArraySet<ComponentName> defaultListeners = mListeners.getDefaultComponents();
for (int i = 0; i < defaultListeners.size(); i++) {
ComponentName cn = defaultListeners.valueAt(i);
allowNotificationListener(userId, cn);
}
ArraySet<String> defaultDnds = mConditionProviders.getDefaultPackages();
//add text 添加需要开机通知权限的包名
defaultDnds.add("com.xx.xx");
//add text
for (int i = 0; i < defaultDnds.size(); i++) {
allowDndPackage(defaultDnds.valueAt(i));
}
setDefaultAssistantForUser(userId);
}
private void allowDndPackage(String packageName) {
try {
getBinderService().setNotificationPolicyAccessGranted(packageName, true);
} catch (RemoteException e) {
e.printStackTrace();
}
}
方案二:(试过不太能行)
./frameworks/base/core/res/res/values/config.xml
通过在 config.xml 或任何原始设备制造商 (OEM) 叠加层中将某个应用的软件包名称添加到 config_defaultListenerAccessPackages,
可以将此应用指定为默认通知监听器。默认通知监听器有权访问所有传入通知,
但用户可以在“设置”应用中的“通知使用权”设置中关闭对通知的使用权。
通知使用权和通知监听器政策 | Android Open Source Project (google.cn)
默认授予通知监听器访问权限,多个用冒号分隔的包名 如:com.xx.xx:com.android.camera2
<!-- Colon separated list of package names that should be granted Notification Listener access -->
<string name="config_defaultListenerAccessPackages" translatable="false">com.xxx.xxx</string>
./frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
@Override
protected void loadDefaultsFromConfig() {
String defaultListenerAccess = mContext.getResources().getString(
R.string.config_defaultListenerAccessPackages);
if (defaultListenerAccess != null) {
//private static final String ENABLED_SERVICES_SEPARATOR = ":";
String[] listeners =
defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR);//用":"分割成数组
for (int i = 0; i < listeners.length; i++) {
if (TextUtils.isEmpty(listeners[i])) {
continue;
}
ArraySet<ComponentName> approvedListeners =
this.queryPackageForServices(listeners[i],
MATCH_DIRECT_BOOT_AWARE
| MATCH_DIRECT_BOOT_UNAWARE, USER_SYSTEM);
for (int k = 0; k < approvedListeners.size(); k++) {
ComponentName cn = approvedListeners.valueAt(k);
addDefaultComponentOrPackage(cn.flattenToString());
}
}
}
}
方案三:给予某个app通知权限(app启动时)
final NotificationManager mgr = context.getSystemService(NotificationManager.class);
mgr.setNotificationPolicyAccessGranted(pkg, access);
--- a/sprd/frameworks/base/core/java/android/app/ActivityThread.java
+++ b/sprd/frameworks/base/core/java/android/app/ActivityThread.java
@@ -242,6 +242,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import android.app.LinkTurboApp;
+import android.app.NotificationManager;
public final class ActivityThread extends ClientTransactionHandler {
//applicationContext.getSystemService(WindowManager.class).getDefaultDisplay();
@Override
public Activity handleLaunchActivity(ActivityClientRecord r,
PendingTransactionActions pendingActions, Intent customIntent) {
unscheduleGcIdler();
mSomeActivitiesChanged = true;
//add text
//uid 权限问题 报异常 java.lang.SecurityException
try{
}catch(Exception e) {
final NotificationManager mgr = mInitialApplication.getApplicationContext().getSystemService(NotificationManager.class);
mgr.setNotificationPolicyAccessGranted("com.xx.xx", true);
}
//add text
if (r.profilerInfo != null) {
mProfiler.setProfiler(r.profilerInfo);
mProfiler.startProfiling();
}
...
方案四:在某个启动的系统服务类,调用setNotificationPolicyAccessGranted(...)
frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
//系统启动,当主要服务启动完毕后,调用systemReady()来启动其它的功能!这里是每次开机必走的!
@Override
public void systemReady() {
// In normal flow, systemReady is called before other system services are ready.
// So it is better not to bind keyguard here.
mKeyguardDelegate.onSystemReady();
...
// add text start
NotificationManager mgr = mContext.getSystemService(NotificationManager.class);
mgr.setNotificationPolicyAccessGranted("com.xx.xx", true);
//ComponentName componentName = new ComponentName("com.xxx.xx","xx.xx.xx.xx");
//mgr.setNotificationListenerAccessGranted(componentName, true);
// add text end
...