我正在尝试扩展AccessibiltyService以拦截通知事件。 我的问题是我的类是由它从未连接到服务创建的。
这就是我的应用的工作原理:
以下是相关课程:
StatCollector:
public class NotificationCollector implements
StatCollectorInterface {
private Context context = null;
private Intent notificationServiceIntent = null;
private boolean collectorStarted = false;
/**Constructor**/
public NotificationCollector(Context context) {
this.context = context;
collectorStarted = false;
}
@Override
public void startCollector() {
if (collectorStarted == true)
{
return;
}
collectorStarted = true;
notificationServiceIntent = new Intent(context,NotificationService.class);
context.startService(notificationServiceIntent);
}
@Override
public void stopCollector() {
if (collectorStarted == false)
{
return;
}
collectorStarted = false;
context.stopService(notificationServiceIntent);
}
NotificationService:
public class NotificationService extends AccessibilityService {
private static final Logger logger = LoggerFactory.getLogger ("Notification");
@Override
public void onCreate()
{
logger.trace("onCreate");
super.onCreate();
}
@Override
public void onDestroy()
{
logger.trace("onDestroy");
super.onDestroy();
}
/**Methods from Accessibility Service**/
@Override
protected void onServiceConnected() {
logger.trace("onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
info.notificationTimeout = 100;
setServiceInfo(info);
}
@Override
public void onInterrupt() {
// TODO Auto-generated method stub
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
logger.trace("onAccessibilityEvent");
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
logger.trace("notification: " + event.getText());
}
}
}
Manifest.xml:(相关组件)
<service android:name=".StatisticsService"/>
<service android:name=".service.NotificationService">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibilityservice" />
</service>
...
<uses-permission android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
请注意,如果我将android.permission.BIND_ACCESSIBILITY_SERVICE添加到NotificationService,应用程序崩溃并出现以下错误
java.lang.RuntimeException: Unable to start service my.app.StatisticsService@406657d0
with Intent { cmp=my.app/.StatisticsService (has extras) }: java.lang.SecurityException:
Not allowed to start service Intent { cmp=my.app/.NotificationService } without permission android.permission.BIND_ACCESSIBILITY_SERVICE
我认为问题在于系统忽略了我的NotificationService类,因为它没有明确提到它具有BIND_ACCESSIBILITY_SERVICE权限。但是,如果我将它添加到清单文件中的服务,则应用程序崩溃。
我错过了什么/做错了什么?
由于
更新
感谢Dr.Cyanide的评论,我发现用户应在设置下的“辅助功能”菜单中为某个应用启用辅助功能。
此外,标签
<uses-permission android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
对于预先的果冻豆版本来说,不是必需的,所以我已经删除了它,因为我正在使用ICS。
现在该应用程序完全按预期工作