NotificationListenerService sendBroadcast无效?

时间:2016-10-10 14:42:01

标签: java android android-intent android-notifications android-broadcast

我现在尝试了超过10个小时,我似乎无法想到其他任何事情。我在互联网上尝试了所有可能的例子,但无济于事。

我有NotificationMonitor类扩展NotificationListenerService,我想使用Intent机制从此服务向主要活动(以及将来可能的其他活动和服务)发送消息。我发布以下代码:

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testpackage.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".NotificationMonitor"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
    </application>

</manifest>

MainActivity.java

public class MainActivity extends Activity {

    private TextView txtView;
    private NotificationReceiver nReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtView = (TextView) findViewById(R.id.textView);

        //create receiver
        nReceiver = new NotificationReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.testpackage.test.NOTIFICATION_MONITOR");
        registerReceiver(nReceiver,filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(nReceiver);
    }

    public void buttonClicked(View v){
        if(v.getId() == R.id.btnTestBroadcast){
            //send test intent without category
            Log.d("ActivityMain","Button clicked");
            Intent i = new Intent("com.testpackage.test.NOTIFICATION_MONITOR");
            sendBroadcast(i);
        }
    }

    class NotificationReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("ActivityMain","Intent received: "+intent.getAction()+" has extra: "+intent.hasExtra("info"));
            if (intent.hasCategory("com.testpackage.test.TEST_CATEGORY")) {
                if (intent.hasExtra("info")) {
                    txtView.setText(intent.getStringExtra("info"));
                }
            }
        }
    }
}

NotificationMonitor.java

public class NotificationMonitor extends NotificationListenerService {

    private NotificationMonitorReceiver receiver;

    @Override
    public void onCreate() {
        super.onCreate();

        receiver = new NotificationMonitorReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.testpackage.test.NOTIFICATION_MONITOR");
        registerReceiver(receiver,filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        //do something
        sendInfo("notification posted");
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        //do something
        sendInfo("notification removed");
    }

    @Override
    public void onListenerConnected() {
        //service created and listener connected
        Log.d("NM","Listener connected!");
        sendInfo("listener connected");
    }

    private void sendInfo(String info) {
        Log.d("NM", "sendInfo called!");
        Intent i = new Intent("com.testpackage.test.NOTIFICATION_MONITOR");
        i.addCategory("com.testpackage.test.TEST_CATEGORY");
        i.putExtra("info", info);
        sendBroadcast(i);
    }

    class NotificationMonitorReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //no categories intents get replied
            Log.d("NM","Intent received: "+intent.getAction()+" has categories: "+(intent.getCategories()!=null));
            if (intent.getCategories() == null) {
                Intent i = new Intent("com.testpackage.test.NOTIFICATION_MONITOR");
                i.addCategory("com.testpackage.test.TEST_CATEGORY");
                sendBroadcast(i);
            }
        }
    }
}

在调试模式下运行此应用程序后,我当然需要重新启用通知权限,所以当我在logcat中看到时:

10-10 16:22:46.428 7330-7381/com.testpackage.test D/NM: Listener connected!
10-10 16:22:46.428 7330-7381/com.testpackage.test D/NM: sendInfo called!
好吧,我应该在我的申请中接收广播,不应该吗? 点击按钮后:

10-10 16:22:57.607 7330-7330/com.testpackage.test D/ActivityMain: Button clicked
10-10 16:22:57.612 7330-7330/com.testpackage.test D/ActivityMain: Intent received: com.testpackage.test.NOTIFICATION_MONITOR has extra: false
10-10 16:22:57.619 7330-7330/com.testpackage.test D/NM: Intent received: com.testpackage.test.NOTIFICATION_MONITOR has categories: false

所以Intent被正确创建并从主活动发送,由同一活动和NotificationListenerService接收回来,没有类别,所以应该得到回复,但没有像调用sendInfo方法时那样发生。

请帮助,我对可能出现的问题没有其他想法。

编辑:我测试了常规服务,当然广播工作得很好。您是否有可能从这个特定的扩展服务类中sendBroadcast出现这种情况?

1 个答案:

答案 0 :(得分:0)

非常,我是个白痴。答案是:我没有在Category中设置IntentFilter过滤器,这就是为什么我收到了来自班级的正确发送的意图。所以,长话短说,要#34;修复&#34;所有人需要做的就是添加:

filter.addCategory("com.testpackage.test.TEST_CATEGORY");

就是这样。谢谢你的关注。