我正在尝试编写一个非常基本的android辅助功能服务,它会显示一条消息,并在任何通知出现时震动。我试着自己在手机上发一封电子邮件来测试它(我想这会显示一些通知)。但我没有看到任何通知。
我的服务代码如下所示
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() { }
}我已确认该服务正在运行,并且已在电话的可访问性菜单中启用了该服务。清单如下所示(删除了一些不相关的部分):
<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只是一个带有启动/停止服务按钮的活动。
发布于 2013-01-27 02:14:02
我在服务标签中添加了一个元数据元素
<meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibilityservice" />在xml资源文件中,我有
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="100" />发布于 2015-12-10 03:56:05
可访问性服务对于您的目的来说过于夸张了,您应该使用NotificationListenerService。对于用户来说,NotificationListenerService更容易实现,也更安全(出于隐私原因)。
发布于 2016-06-22 17:58:31
配置可访问性服务的另一种方法是从java代码覆盖AccessibilityService的OnServiceConnected()方法。
@Override
protected void onServiceConnected() {
AccessibilityServiceInfo info = getServiceInfo();
info.packageNames = new String[]
{
"com.packagename1","com.packagename2"
};
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
this.setServiceInfo(info);
}参考
https://stackoverflow.com/questions/9675611
复制相似问题