Android Enable/Disable Notification access Automat

2019-09-19 23:44发布

Hi how can I enable/disable the notification access settings for my app automatically?

2条回答
在下西门庆
2楼-- · 2019-09-20 00:05

I think it is not possible.You have to go settings and u have to on.You can give intent to settings like

startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));

查看更多
来,给爷笑一个
3楼-- · 2019-09-20 00:22

It can be done using following code:

ContentResolver cr = context.getContentResolver();
String setting = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS;
String permissionString = Settings.Secure.getString(cr, setting);
if(permissionString == null || !permissionString.contains("com.abc.xyz"))
{
    ComponentName analytics = new ComponentName("com.abc.xyz", "com.abc.xyz.LMN");
    if(permissionString == null)
        permissionString = "";
    else
        permissionString += ":";
    permissionString += analytics.flattenToString();
    Settings.Secure.putString(cr, setting, permissionString);
}

Note that the application needs to be a System application or signed with the same key used by framework res apk in order write in Setting.Secure database.

查看更多
登录 后发表回答