getActiveNotifications

getActiveNotifications

本文介绍了使用 NotificationListenerService 时 getActiveNotifications 始终为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了这个kpBird的示例代码和这个开发者指南

我可以:

  • 调用此服务的意图.

  • Call intent to this service.

可以从通知中捕捉广播.

Can catch Broadcast from Notification.

所以我得到了错误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 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 + "");
        }
    }

这是从 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() + "	" + sbn.getNotification().tickerText + "	" + sbn.getPackageName());

        Intent i = new  Intent(Notification.NOTIFICATION);
        i.putExtra("notification_event","onNotificationPosted :" + sbn.getPackageName() + "
");
        sendBroadcast(i);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        Log.i(TAG,"onNOtificationRemoved");
        Log.i(TAG,"ID :" + sbn.getId() + "	" + sbn.getNotification().tickerText +"	" + sbn.getPackageName());

        Intent i = new  Intent(Notification.NOTIFICATION);
        i.putExtra("notification_event","onNotificationRemoved :" + sbn.getPackageName() + "
");

        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() + "
");
                    sendBroadcast(i2);
                    i++;
                }

                Intent i3 = new  Intent(Notification.NOTIFICATION);
                i3.putExtra("notification_event","===== Notification List ====");
                sendBroadcast(i3);

            }
        }
    }
}

推荐答案

在我在 Settings 的 Notification Access 部分启用此应用程序后 ->安全.

After I enable this application in Notification Access section in Settings -> Security.

我现在可以从 getActiveNotifications 接收数据.

I can received data from getActiveNotifications now.

谢谢,

这篇关于使用 NotificationListenerService 时 getActiveNotifications 始终为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 23:52