我跟着这个Sample Code of kpBird这个Developer Guide
我可以:
调用此服务的意图。
可以从通知捕获广播。
所以我得到了错误getActiveNotifications always null
。
我不知道为什么,
知道的人,请帮帮我,
谢谢,
这是我得到的源代码和错误。
错误:
04-28 08:46:11.625: E/AndroidRuntime(7651): FATAL EXCEPTION: main
04-28 08:46:11.625: E/AndroidRuntime(7651): java.lang.RuntimeException: Error receiving broadcast Intent { act=app.trekband.NotificationListener flg=0x10 (has extras) } in utils.NotificationListener$NLServiceReceiver@42680780
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.os.Handler.handleCallback(Handler.java:730)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.os.Looper.loop(Looper.java:176)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-28 08:46:11.625: E/AndroidRuntime(7651): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 08:46:11.625: E/AndroidRuntime(7651): at java.lang.reflect.Method.invoke(Method.java:525)
04-28 08:46:11.625: E/AndroidRuntime(7651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-28 08:46:11.625: E/AndroidRuntime(7651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-28 08:46:11.625: E/AndroidRuntime(7651): at dalvik.system.NativeStart.main(Native Method)
04-28 08:46:11.625: E/AndroidRuntime(7651): Caused by: java.lang.NullPointerException
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.os.Parcel.readException(Parcel.java:1437)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.os.Parcel.readException(Parcel.java:1385)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:518)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
04-28 08:46:11.625: E/AndroidRuntime(7651): at utils.NotificationListener$NLServiceReceiver.onReceive(NotificationListener.java:73)
04-28 08:46:11.625: E/AndroidRuntime(7651): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
04-28 08:46:11.625: E/AndroidRuntime(7651): ... 9 more
源代码:
我用这个调用了
NotificationListener class
:private NotificationReceiver notificationReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!Constants.connectivity.isNetworkOnline()) {
// If there is error
Toast.makeText(getActivity(), getString(R.string.toast_failed_connection),
Toast.LENGTH_SHORT).show();
// Exit the application
android.os.Process.killProcess(android.os.Process.myPid());
}
Intent intent = new Intent(getActivity(), NotificationListener.class);
getActivity().startService(intent);
notificationReceiver = new NotificationReceiver();
IntentFilter filter = new IntentFilter();
// Add action
filter.addAction(Notification.NOTIFICATION);
// Register receiver
getActivity().registerReceiver(notificationReceiver,filter);
}
public class NotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String temp = intent.getStringExtra("notification_event");
// CURRENTLY THIS VALUE IS NULL
Log.i(TAG, temp + "");
}
}
这是从
NotificationListener
扩展到NotificationListenerService
的类。public class NotificationListener extends NotificationListenerService{
private String TAG = NotificationListener.class.getSimpleName();
private NLServiceReceiver mNLServiceReciver;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
mNLServiceReciver = new NLServiceReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Notification.NOTIFICATION);
registerReceiver(mNLServiceReciver,filter);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
unregisterReceiver(mNLServiceReciver);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"onNotificationPosted");
Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName());
Intent i = new Intent(Notification.NOTIFICATION);
i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "\n");
sendBroadcast(i);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG,"onNOtificationRemoved");
Log.i(TAG,"ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText +"\t" + sbn.getPackageName());
Intent i = new Intent(Notification.NOTIFICATION);
i.putExtra("notification_event","onNotificationRemoved :" + sbn.getPackageName() + "\n");
sendBroadcast(i);
}
public class NLServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra("command").equals("clearall")){
NotificationListener.this.cancelAllNotifications();
} else if(intent.getStringExtra("command").equals("list")){
Intent i1 = new Intent(Notification.NOTIFICATION);
i1.putExtra("notification_event","=====================");
sendBroadcast(i1);
// ERROR HERE
Log.i(TAG, "getActiveNotifications " + getActiveNotifications());
Log.i(TAG, "length " + getActiveNotifications().length);
int i=1;
for (StatusBarNotification sbn : NotificationListener.this.getActiveNotifications()) {
Intent i2 = new Intent(Notification.NOTIFICATION);
i2.putExtra("notification_event",i +" " + sbn.getPackageName() + "\n");
sendBroadcast(i2);
i++;
}
Intent i3 = new Intent(Notification.NOTIFICATION);
i3.putExtra("notification_event","===== Notification List ====");
sendBroadcast(i3);
}
}
}
}
最佳答案
在Notification Access
的Settings -> Security
部分中启用此应用程序后。
我现在可以从getActiveNotifications
接收数据。
谢谢,