赞
踩
当大家看到这篇文章的时候,应该已经看到过很多关于开机自启动的文章,大概可以归纳为
开机广播方案、产商定制的自启动功能方案
如文章标题,这篇文章会提到一种新的方案:
无障碍开机自启动
说到这里,大家可能有点迷惑,简单讲就是当应用开启无障碍之后,开机就会自启动了。说到这文章其实也该结束了。但是介于无障碍服务属于一个特殊的系统服务,我觉得有必要再多讲一点关于其授权的方法,毕竟网络上已经有很多文章教我们从0到1开启一个无障碍服务,但很少讲到如何授权。
接下来,我会谈到无障碍服务的几种授权方案:(主要针对root或系统权限)
- public class KeepAliveHelper {
- private static boolean isAccessibilitySettingsOn(String componentName, Context context) {
- if (TextUtils.isEmpty(componentName)) return false;
- int accessibilityEnable = 0;
- try {
- accessibilityEnable = Settings.Secure.getInt(context.getContentResolver(),
- Settings.Secure.ACCESSIBILITY_ENABLED, 0);
- } catch (Exception ignored) {}
- try {
- if (accessibilityEnable == 1) {
- TextUtils.SimpleStringSplitter mStringColonSplitter =
- new TextUtils.SimpleStringSplitter(':');
- String settingValue = Settings.Secure.getString(context.getContentResolver(),
- Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
- if (settingValue != null) {
- mStringColonSplitter.setString(settingValue);
- while (mStringColonSplitter.hasNext()) {
- String accessibilityService = mStringColonSplitter.next();
- if (accessibilityService.equalsIgnoreCase(componentName)) {
- return true;
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
-
- public static boolean openAccessibilityService(Context context, String componentName){
- if (TextUtils.isEmpty(componentName)) return false;
- if (isAccessibilitySettingsOn(componentName, context)){
- return true;
- }
- String enabledAccessibilityStr = Settings.Secure.getString(context.getContentResolver(),
- Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
- String text = ComponentName.unflattenFromString(componentName).flattenToShortString();
- if (TextUtils.isEmpty(text)) return false;
- if (!TextUtils.isEmpty(enabledAccessibilityStr)){
- text = enabledAccessibilityStr + ":" + text;
- }
- String cmd = String.format("settings put secure enabled_accessibility_services %s && settings put secure accessibility_enabled 1", text);
- return new ShellCommand().execute(cmd, true).result == 0;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。