问题描述
我跟着这个的rel="nofollow">样品code和该开发人员指南
我可以:
-
通话意图这项服务。
-
可以从通知赶上广播。
所以,我得到了错误 getActiveNotifications总是空
。
我不知道为什么,
谁认识的人,请大家帮帮我,
谢谢
P / S:这是源$ C $ c和错误,我得到的
。错误:
8月4日至28号:46:11.625:E / AndroidRuntime(7651):致命异常:主要
八月四号至28日:46:11.625:E / AndroidRuntime(7651):java.lang.RuntimeException的:接收广播错误意向{行动= app.trekband.NotificationListener FLG = 0×10(有临时演员)}在utils.NotificationListener$NLServiceReceiver@42680780
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:778)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.os.Handler.handleCallback(Handler.java:730)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.os.Handler.dispatchMessage(Handler.java:92)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.os.Looper.loop(Looper.java:176)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.app.ActivityThread.main(ActivityThread.java:5419)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在java.lang.reflect.Method.invokeNative(本机方法)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在java.lang.reflect.Method.invoke(Method.java:525)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在dalvik.system.NativeStart.main(本机方法)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):由:显示java.lang.NullPointerException
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.os.Parcel.readException(Parcel.java:1437)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.os.Parcel.readException(Parcel.java:1385)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:518)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在utils.NotificationListener $ NLServiceReceiver.onReceive(NotificationListener.java:73)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:768)
八月四号至28日:46:11.625:E / AndroidRuntime(7651):9 ...更多
来源$ C $ C:
我叫的NotificationListener类
通过使用这样的:
私人NotificationReceiver notificationReceiver;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
如果(!Constants.connectivity.isNetworkOnline()){
//如果有错误
Toast.makeText(getActivity(),的getString(R.string.toast_failed_connection),
Toast.LENGTH_SHORT).show();
//退出应用程序
android.os.Process.killProcess(android.os.Process.myPid());
}
意向意图=新的意图(getActivity(),NotificationListener.class);
。getActivity()startService(意向);
notificationReceiver =新NotificationReceiver();
IntentFilter的过滤器=新的IntentFilter();
//添加行为
filter.addAction(Notification.NOTIFICATION);
//注册接收器
。getActivity()registerReceiver(notificationReceiver,过滤器);
}
公共类NotificationReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
字符串临时= intent.getStringExtra(notification_event);
//目前,该值为NULL
Log.i(TAG,温度+);
}
}
这是的NotificationListener
类从 NotificationListenerService
公共类的NotificationListener扩展NotificationListenerService {
私人字符串变量= NotificationListener.class.getSimpleName();
私人NLServiceReceiver mNLServiceReciver;
@覆盖
公共无效的onCreate(){
super.onCreate();
Log.i(TAG的onCreate);
mNLServiceReciver =新NLServiceReceiver();
IntentFilter的过滤器=新的IntentFilter();
filter.addAction(Notification.NOTIFICATION);
registerReceiver(mNLServiceReciver,过滤器);
}
@覆盖
公共无效的onDestroy(){
super.onDestroy();
Log.i(TAG的onDestroy);
unregisterReceiver(mNLServiceReciver);
}
@覆盖
公共无效onNotificationPosted(StatusBarNotification SBN){
Log.i(TAG,onNotificationPosted);
Log.i(TAG,ID:+ sbn.getId()+\ t+ sbn.getNotification()tickerText +\ t+ sbn.getPackageName());
意图I =新的意图(Notification.NOTIFICATION);
i.putExtra(notification_event,onNotificationPosted:+ sbn.getPackageName()+\ N);
sendBroadcast(ⅰ);
}
@覆盖
公共无效onNotificationRemoved(StatusBarNotification SBN){
Log.i(TAG,onNOtificationRemoved);
Log.i(TAG,ID:+ sbn.getId()+\ t+ sbn.getNotification()tickerText +\ t+ sbn.getPackageName());
意图I =新的意图(Notification.NOTIFICATION);
i.putExtra(notification_event,onNotificationRemoved:+ sbn.getPackageName()+\ N);
sendBroadcast(ⅰ);
}
公共类NLServiceReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getStringExtra(命令)。等于(clearall)){
NotificationListener.this.cancelAllNotifications();
}否则,如果(intent.getStringExtra(命令)。等于(名单)){
意图I1 =新的意图(Notification.NOTIFICATION);
i1.putExtra(notification_event,=====================);
sendBroadcast(I1);
//错误这里
Log.i(TAG,getActiveNotifications+ getActiveNotifications());
Log.i(TAG,长度+ getActiveNotifications()的长度。);
INT I = 1;
为(StatusBarNotification SBN:NotificationListener.this.getActiveNotifications()){
意图I2 =新的意图(Notification.NOTIFICATION);
i2.putExtra(notification_event,我++ sbn.getPackageName()+\ N);
sendBroadcast(I 2);
我++;
}
意图I3 =新的意图(Notification.NOTIFICATION);
i3.putExtra(notification_event,=====通知列表====);
sendBroadcast(I3);
}
}
}
}
在我启用通知访问这个应用程序
在部分设置 - >安全
。
我可以从接收到的数据 getActiveNotifications
了。
谢谢
I followed this Sample Code of kpBird and this Developer Guide
I can :
Call intent to this service.
Can catch Broadcast from Notification.
So I got the error getActiveNotifications always null
.
I don't know why,
People who know, please help me,
Thanks,
p/s : Here is source code and error I get.
Error :
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
Source Code :
I called NotificationListener class
by using this :
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 + "");
}
}
This is NotificationListener
class which extends from 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);
}
}
}
}
After I enable this application in Notification Access
section in Settings -> Security
.
I can received data from getActiveNotifications
now.
Thanks,
这篇关于getActiveNotifications使用NotificationListenerService时,总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!