gpt4 book ai didi

android - 使用 NotificationListenerService 时 getActiveNotifications 始终为 null

转载 作者:太空狗 更新时间:2023-10-29 15:46:20 24 4
gpt4 key购买 nike

我关注了这个Sample Code of kpBird还有这个Developer Guide

我可以:

  • 调用此服务的 Intent 。

  • 可以从通知中捕获广播。

所以我得到了错误getActiveNotifications always null

我不知道为什么,

请知道的人帮帮我,

谢谢,

p/s : 这是源代码和我得到的错误。

错误:

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 类:

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 + "");
}
}

这是从 NotificationListenerService 扩展而来的 NotificationListener

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);

}
}
}
}

最佳答案

Settings -> SecurityNotification Access 部分启用此应用程序后。

我现在可以从 getActiveNotifications 接收数据了。

谢谢,

关于android - 使用 NotificationListenerService 时 getActiveNotifications 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com