Android辅助服务 [英] Android accessibility service
问题描述
我试图写一个非常基本的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.
我的服务code看起来像
My service code looks like
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" />
这篇关于Android辅助服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!