Android O - 指纹手势回调不起作用

标签 android android-gesture android-fingerprint-api android-8.0-oreo accessibility

我正在 Pixel 设备上通过辅助功能开启指纹手势进行测试。我正在尝试使用 FingerprintGestureController 获取手势回调,但即使在我从“设置”->“辅助功能”中打开此应用程序的辅助功能后,也从未获得任何手势作为返回。 isGestureDetectionAvailable() 总是向我返回 false。有人可以帮忙吗。

代码如下:

my_gesture_service.xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRequestFingerprintGestures="true" />

AndroidManifest.xml

<uses-feature android:name="android.hardware.fingerprint"/>
    <uses-permission android:name="android.permission.USE_FINGERPRINT" />
    <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<service
            android:name="android.gestures.MyService"
            android:label="@string/app_name"
            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/my_gesture_service" />
        </service>

MyService.java

public class MyService extends AccessibilityService {

    private static final String TAG = MyService.class.getSimpleName();

    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        Log.d(TAG, "onAccessibilityEvent");
    }

    @Override
    public void onInterrupt() {
        Log.d(TAG, "onInterrupt");
    }

    @Override
    protected boolean onGesture(int gestureId) {
        Log.d(TAG, "onGesture " + gestureId);
        return super.onGesture(gestureId);
    }

    @Override
    protected boolean onKeyEvent(KeyEvent event) {
        Log.d(TAG, "onKeyEvent " + event.getKeyCode());
        return super.onKeyEvent(event);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(), "onDestroy" , Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }



    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG, "onServiceConnected");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            FingerprintGestureController gestureController = getFingerprintGestureController();

            Toast.makeText(getApplicationContext(), "Is available: " + gestureController.isGestureDetectionAvailable(), Toast.LENGTH_LONG).show();
            Log.e(TAG, "Is available: " + gestureController.isGestureDetectionAvailable() );

            FingerprintGestureController.FingerprintGestureCallback callback = new
                    FingerprintGestureController.FingerprintGestureCallback() {
                        @Override
                        public void onGestureDetectionAvailabilityChanged(boolean available) {
                            super.onGestureDetectionAvailabilityChanged(available);
                            Toast.makeText(getApplicationContext(), "Gesture available change to: " + available, Toast.LENGTH_SHORT).show();
                            Log.d(TAG, "onGestureDetectionAvailabilityChanged " + available);
                        }

                        @Override
                        public void onGestureDetected(int gesture) {
                            super.onGestureDetected(gesture);
                            Toast.makeText(getApplicationContext(), "Gesture: " + gesture, Toast.LENGTH_SHORT).show();
                            Log.d(TAG, "onGestureDetected " + gesture);
                        }
                    };

            gestureController.registerFingerprintGestureCallback(callback, new Handler());
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind " );
        return super.onUnbind(intent);
    }


}

最佳答案

找到了答案。我在 xml 中也需要这个标志:

android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"

关于Android O - 指纹手势回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125945/

相关文章:

java - 使用 Picasso 加载的图像在滚动时被删除并不断重新加载

android - 带有按钮的自定义 View 实现 OnGestureListener

android - 在运行时更改手势颜色

Android 如何在没有 onDown true 的情况下在 LinearLayout 上添加滑动手势

android - 在后台持续监听 Android 设备上的指纹

java - 适用于 Android 指纹的 Aadhar 身份验证 RD 服务

java - 为什么我的 java 文件看不到 R 文件中的新元素?

android - onPostExecute 中的 AlertDialog 未显示

android - 文本在 TextView 中看不清楚?

android - 为 android 和 ios react native 指纹认证