原创

android4.2中将App添加到Accessbility Service中,并默认开启;

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wch18956441322/article/details/39956627

当应用程序需要获取通知栏读取的权限的时候,我们需要用户手动在Settings-Accessbility-Servie项下手动将我们的app开启权限;当有系统权限时,我们可以默认将某个app权限开启;


其实也就是更改数据库中的值;

SettingsProviders的secure表中的enabled_accessibility_services字段:

110|enabled_accessibility_services|×××××PackageName/××××AssessibilityService

private static final String SERVICE_NAME = "×××××PackageName/××××AssessibilityService"
private void updateAccessibility() {
        // Parse the enabled services.
        Set<ComponentName> enabledServices = getEnabledServicesFromSettings(mContext);
        if(null == enabledServices) {
        	return;
        }
        // Determine enabled services and accessibility state.
        ComponentName toggledService = ComponentName.unflattenFromString(SERVICE_NAME);
        final boolean accessibilityEnabled;
        // Enabling at least one service enables accessibility.
        accessibilityEnabled = true;
        enabledServices.add(toggledService);
        // Update the enabled services setting.
        StringBuilder enabledServicesBuilder = new StringBuilder();
        // Keep the enabled services even if they are not installed since we
        // have no way to know whether the application restore process has
        // completed. In general the system should be responsible for the
        // clean up not settings.
        for (ComponentName enabledService : enabledServices) {
            enabledServicesBuilder.append(enabledService.flattenToString());
            enabledServicesBuilder.append(ENABLED_ACCESSIBILITY_SERVICES_SEPARATOR);
        }
        final int enabledServicesBuilderLength = enabledServicesBuilder.length();
        if (enabledServicesBuilderLength > 0) {
            enabledServicesBuilder.deleteCharAt(enabledServicesBuilderLength - 1);
        }
        Settings.Secure.putString(mContext.getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                enabledServicesBuilder.toString());

        // Update accessibility enabled.
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_ENABLED, accessibilityEnabled ? 1 : 0);
    }


private static Set<ComponentName> getEnabledServicesFromSettings(Context context) {
        String enabledServicesSetting = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        if (enabledServicesSetting == null) {
            enabledServicesSetting = "";
        }
        Set<ComponentName> enabledServices = new HashSet<ComponentName>();
        SimpleStringSplitter colonSplitter = sStringColonSplitter;
        colonSplitter.setString(enabledServicesSetting);
        while (colonSplitter.hasNext()) {
            String componentNameString = colonSplitter.next();
            ComponentName enabledService = ComponentName.unflattenFromString(
                    componentNameString);
            if (enabledService != null) {
            	if(enabledService.flattenToString().equals(SERVICE_NAME)) {
            		return null;
            	}
                enabledServices.add(enabledService);
            }
        }
        return enabledServices;
    }


文章最后发布于: 2014-10-10 08:46:31
展开阅读全文
0 个人打赏

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览