安卓无障碍服务 [英] Android accessibility service

查看:51
本文介绍了安卓无障碍服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个非常基本的 android 无障碍服务,该服务会在出现任何通知时显示消息并振动.我已经尝试通过自己在手机上发送电子邮件来测试它(我认为这会显示一些通知).但是我没有看到任何通知.

I'm trying to write a very basic android accessibility service that shows a message and vibrates when any notification is raised. I've tried testing it by sending an email myself on my phone (I figured that would show some notification). But I have not seen any notifications.

我的服务代码看起来像

public class NotifierService extends AccessibilityService {

  @Override
  public void onAccessibilityEvent(AccessibilityEvent evt) {
    Toast.makeText(this, "Got event from " + evt.getPackageName(), Toast.LENGTH_SHORT)
            .show();
    Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    v.vibrate(new long[] { 0, 250, 250, 250, 250, 250, 250, 250, 250 }, -1);
  }

  @Override
  public void onInterrupt() { }
}

我已验证该服务正在运行,并已在手机的辅助功能菜单中启用它.清单看起来像这样(删除了一些不相关的部分):

I have verified that the service is running and I have enabled it in the accessibility menu of the phone. And the manifest looks like this (some parts are removed that are not relevant):

<uses-permission android:name="android.permission.VIBRATE" />
<application>
    <activity
        android:name=".MyActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".NotifierService" android:enabled="true" >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
    </service>
</application>

MyActivity 只是一个带有启动/停止服务按钮的活动.

MyActivity is just an activity with a button for starting/stopping the service.

推荐答案

我在服务标签内添加了一个元数据元素

I added a meta data element inside the service tag

<meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" />

在我有的 xml 资源文件中

And in the xml resources file I have

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
                       android:accessibilityEventTypes="typeNotificationStateChanged"
                       android:accessibilityFeedbackType="feedbackAllMask"
                       android:notificationTimeout="100" />

这篇关于安卓无障碍服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆