编辑2:我刚刚意识到我没有用新的 AccessibilityServiceInfo 初始化信息,我只是愚蠢。
我正在尝试使用 AccessibilityService 来收听和检索从我的其他应用程序到手机(youtube、facebook、sms)的通知信息
它崩溃并且 logcat 在 onServiceConnected 上报告空指针异常
谷歌搜索后,我认为这可能是由于活动尚未绑定到服务,因此无法启动它?
我需要绑定无障碍服务吗?
如果是这样,我该怎么做?
我的 AccessibilityListener 类中有这些代码
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent(BROADCAST_ACTION);
Log.d("TAG","onCreate");
}
@Override
public void onServiceConnected() {
info.packageNames = new String[] {"com.whatsapp","com.facebook"};
info.notificationTimeout = 100;
this.setServiceInfo(info);
Log.d("TAG","onServiceConnected");
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
NotificationInfo info = new NotificationInfo();
Toast.makeText(getApplicationContext(), "Received event", 1000);
Log.d("TAG","onaccessibility event running");
switch (eventType) {
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
info.packageName = (String) event.getPackageName();
info.eventTime = event.getEventTime();
info.eventText = event.getText().toString();
sendEvent(info);
}
}
以及我活动中的这些代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//sets the main layout of the activity
setContentView(R.layout.calendar_display);
//initializes the calendarview
initializeCalendar();
intent = new Intent(this,AccessibilityListener.class);
}
@Override
protected void onStart() {
super.onStart();
// Bind to Your Service
// Intent intent = new Intent(this, AccessibilityListener.class);
LocalBroadcastManager.getInstance(this).registerReceiver(notificationReceiver,
new IntentFilter("add-event"));
startService(intent);
}
@Override
protected void onResume() {
super.onResume();
if(isMyServiceRunning(AccessibilityListener.class))
Toast.makeText(this,"Service is online", 1000).show();
else
Toast.makeText(this,"Service is offline", 1000).show();
registerReceiver(notificationReceiver,new IntentFilter(AccessibilityListener.BROADCAST_ACTION));
}
编辑:根据要求,在 logcat 中添加
> 08-25 07:35:29.265: D/ActivityThread(24456): handleBindApplication:com.example.utarapp
08-25 07:35:29.265: D/ActivityThread(24456): setTargetHeapUtilization:0.75
08-25 07:35:29.265: D/ActivityThread(24456): setTargetHeapMinFree:2097152
08-25 07:35:29.630: D/TAG(24456): onCreate
08-25 07:35:29.715: D/libEGL(24456): loaded /system/lib/egl/libEGL_mali.so
08-25 07:35:29.725: D/libEGL(24456): loaded /system/lib/egl/libGLESv1_CM_mali.so
08-25 07:35:29.730: D/libEGL(24456): loaded /system/lib/egl/libGLESv2_mali.so
08-25 07:35:29.730: E/(24456): Device driver API match
08-25 07:35:29.730: E/(24456): Device driver API version: 23
08-25 07:35:29.730: E/(24456): User space API version: 23
08-25 07:35:29.730: E/(24456): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
08-25 07:35:29.830: D/OpenGLRenderer(24456): Enabling debug mode 0
08-25 07:35:30.150: D/AndroidRuntime(24456): Shutting down VM
08-25 07:35:30.150: W/dalvikvm(24456): threadid=1: thread exiting with uncaught exception (group=0x4185ace0)
08-25 07:35:30.155: E/AndroidRuntime(24456): FATAL EXCEPTION: main
08-25 07:35:30.155: E/AndroidRuntime(24456): Process: com.example.utarapp, PID: 24456
08-25 07:35:30.155: E/AndroidRuntime(24456): java.lang.NullPointerException
08-25 07:35:30.155: E/AndroidRuntime(24456): at com.example.utarapp.AccessibilityListener.onServiceConnected(AccessibilityListener.java:36)
08-25 07:35:30.155: E/AndroidRuntime(24456): at android.accessibilityservice.AccessibilityService$1.onServiceConnected(AccessibilityService.java:546)
08-25 07:35:30.155: E/AndroidRuntime(24456): at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:659)
08-25 07:35:30.155: E/AndroidRuntime(24456): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
08-25 07:35:30.155: E/AndroidRuntime(24456): at android.os.Handler.dispatchMessage(Handler.java:102)
08-25 07:35:30.155: E/AndroidRuntime(24456): at android.os.Looper.loop(Looper.java:136)
08-25 07:35:30.155: E/AndroidRuntime(24456): at android.app.ActivityThread.main(ActivityThread.java:5146)
08-25 07:35:30.155: E/AndroidRuntime(24456): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 07:35:30.155: E/AndroidRuntime(24456): at java.lang.reflect.Method.invoke(Method.java:515)
08-25 07:35:30.155: E/AndroidRuntime(24456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
08-25 07:35:30.155: E/AndroidRuntime(24456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
08-25 07:35:30.155: E/AndroidRuntime(24456): at dalvik.system.NativeStart.main(Native Method)
08-25 07:35:32.080: I/Process(24456): Sending signal. PID: 24456 SIG: 9
08-25 07:35:33.165: D/ActivityThread(24700): handleBindApplication:com.example.utarapp
08-25 07:35:33.165: D/ActivityThread(24700): setTargetHeapUtilization:0.75
08-25 07:35:33.165: D/ActivityThread(24700): setTargetHeapMinFree:2097152
08-25 07:35:33.225: D/TAG(24700): onCreate
08-25 07:35:33.235: D/AndroidRuntime(24700): Shutting down VM
08-25 07:35:33.235: W/dalvikvm(24700): threadid=1: thread exiting with uncaught exception (group=0x4185ace0)
08-25 07:35:33.240: E/AndroidRuntime(24700): FATAL EXCEPTION: main
08-25 07:35:33.240: E/AndroidRuntime(24700): Process: com.example.utarapp, PID: 24700
08-25 07:35:33.240: E/AndroidRuntime(24700): java.lang.NullPointerException
08-25 07:35:33.240: E/AndroidRuntime(24700): at com.example.utarapp.AccessibilityListener.onServiceConnected(AccessibilityListener.java:36)
08-25 07:35:33.240: E/AndroidRuntime(24700): at android.accessibilityservice.AccessibilityService$1.onServiceConnected(AccessibilityService.java:546)
08-25 07:35:33.240: E/AndroidRuntime(24700): at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:659)
08-25 07:35:33.240: E/AndroidRuntime(24700): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
08-25 07:35:33.240: E/AndroidRuntime(24700): at android.os.Handler.dispatchMessage(Handler.java:102)
08-25 07:35:33.240: E/AndroidRuntime(24700): at android.os.Looper.loop(Looper.java:136)
08-25 07:35:33.240: E/AndroidRuntime(24700): at android.app.ActivityThread.main(ActivityThread.java:5146)
08-25 07:35:33.240: E/AndroidRuntime(24700): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 07:35:33.240: E/AndroidRuntime(24700): at java.lang.reflect.Method.invoke(Method.java:515)
08-25 07:35:33.240: E/AndroidRuntime(24700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
08-25 07:35:33.240: E/AndroidRuntime(24700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
08-25 07:35:33.240: E/AndroidRuntime(24700): at
dalvik.system.NativeStart.main(Native Method)
08-25 07:40:33.305: I/Process(24700): Sending signal. PID: 24700 SIG: 9