用于手势屏幕锁定的Android辅助功能服务

时间:2017-07-04 09:45:04

标签: android accessibility gesture

我正在创建我的手势锁定屏幕,一旦屏幕开启就会显示该屏幕。

无法在\ off广播接收器上使用屏幕,因为如果从清单注册,它们将无法工作。并且你无法一直保持服务运行。

我的服务看起来像。

public class CustomAccessibilityService extends AccessibilityService {

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    // event.getSource(), findFocus(int), getWindows(), or getRootInActiveWindow().

    Toast.makeText(this, "onAccessibilityEvent" + event.getSource(), Toast.LENGTH_SHORT).show();
}

@Override
public void onInterrupt() {
    Toast.makeText(this, "onInterrupt", Toast.LENGTH_SHORT).show();
}

@Override
protected boolean onGesture(int gestureId) {
    Toast.makeText(this, "onGesture" + gestureId, Toast.LENGTH_SHORT).show();
    return super.onGesture(gestureId);
}

}

清单文件

<service
        android:name=".CustomAccessibilityService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/service_configuration" />
    </service>

和Service_configuration.xml如下。

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewAccessibilityFocused|typeViewAccessibilityFocusCleared"
android:accessibilityFeedbackType="feedbackAllMask"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100"
android:packageNames="com.xxxx.gesturelockscreen" />

我被困在上面。

我知道我们需要激活辅助功能服务(手势锁定屏幕),我使用下面的代码来检查它是否已启用。

 if (!isAccessibilityEnabled()) {
        Toast.makeText(this, "Not Enabled!", Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        startActivity(intent);

    } else {
 //Good to go!
  }

public boolean isAccessibilityEnabled() {
    int accessibilityEnabled = 0;
    final String ACCESSIBILITY_SERVICE_NAME = "com.xxx.gesturelockscreen/com.xxx.gesturelockscreen.CustomAccessibilityService";
    boolean accessibilityFound = false;
    try {
        accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
        Log.d(TAG, "ACCESSIBILITY: " + accessibilityEnabled);
    } catch (Settings.SettingNotFoundException e) {
        Log.d(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
    }

    TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

    if (accessibilityEnabled == 1) {
        Log.d(TAG, "***ACCESSIBILIY IS ENABLED***: ");


        String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
        Log.d(TAG, "Setting: " + settingValue);
        if (settingValue != null) {
            TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
            splitter.setString(settingValue);
            while (splitter.hasNext()) {
                String accessabilityService = splitter.next();
                Log.d(TAG, "Setting: " + accessabilityService);
                if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)) {
                    Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
                    return true;
                }
            }
        }

        Log.d(TAG, "***END***");
    } else {
        Log.d(TAG, "***ACCESSIBILIY IS DISABLED***");
    }
    return accessibilityFound;
}

现在我希望屏幕在我的自定义辅助功能服务上时需要调用。

请提供帮助,感谢任何帮助,提前谢谢。

0 个答案:

没有答案