android - 使用 NotificationListenerService 时 getActiveNotifications 始终为 null

标签 android service notifications broadcastreceiver android-notifications

我关注了这个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/

相关文章:

android - 如何使用 Android 连接蓝牙打印机?

c# - 如何将 Json 文件添加到 Xamarin 表单?

android - 绑定(bind)到 Android 中的服务

java - 作为 Upstart 服务启动时无法读取 UTF-8 文件名

typescript - 类型不可分配(TypeScript 4.1.2)

android - Xamarin Forms 项目模拟器和设备上的空白谷歌地图

android - android中的 ListView 空指针异常

windows-7 - 是否可以在不重新启动的情况下卸载内核驱动程序?

android - 在 NotificationListenerService 中的 onNotificationPosted 获取 Notification 的 contentintent?

design-patterns - 在通知系统中每个通知插入多少行?