gpt4 book ai didi

java - NotificationListenerService sendBroadcast 不起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:45 25 4
gpt4 key购买 nike

我现在已经尝试了 10 多个小时,但我似乎无法考虑任何其他事情。我尝试了互联网上所有可能的示例,但没有成功。

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

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

在 Debug模式下运行此应用程序后,我当然需要重新启用通知权限,因此当我这样做时,我会在 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 已正确创建并从主 Activity 发送,由同一 Activity 和NotificationListenerService接收回来,没有类别,因此应该得到回复,但没有像调用 sendInfo 方法时那样发生。

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

编辑:我用常规服务进行了测试,当然广播工作得很好。您是否有可能无法从这个特定的扩展服务类sendBroadcast

最佳答案

从官方角度来说,我是个白痴。答案是:我没有在 IntentFilter 中设置 Category 过滤器,这就是为什么我从类(class)收到零个正确发送的 Intent 。因此,长话短说,要“修复”这个错误,只需添加:

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

就是这样。感谢您的关注。

关于java - NotificationListenerService sendBroadcast 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960880/

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