- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我现在已经尝试了 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/
我在从作为系统应用程序加载到自定义 ROM 中的应用程序发送 Broadcast 时遇到问题(使用 android:sharedUserId="android.uid.system" 在 Manife
sendBroadcast() - 它应该在 Activity 中调用吗?我正在尝试从不扩展 Activity 的实用程序类方法调用 sendBroadcast()。我收到如下编译错误 The met
我有一个广播来检查网络,当网络改变状态时,我用 onNetworkActivated() 或 onNetworkInactivated() 通知我的 Activity : public class N
我有一个列表,我试图使用 Intent 来广播它。在学习了在线教程后,建议我使用 Parcelable 来发送此数据。但是,我在 logcat 中不断收到此错误: Caused by: java.la
我正在使用 sendBroadcast 进行媒体文件扫描。然后我需要等到完成sendBroadcast 之后。我如何在 Android 中执行此操作? 我知道我可以在这里使用简单的 while 逻辑,
我现在已经尝试了 10 多个小时,但我似乎无法考虑任何其他事情。我尝试了互联网上所有可能的示例,但没有成功。 我有扩展NotificationListenerService的NotificationM
很想知道 sendBroadcast 的内部工作原理,如果有人能解释一下就好了。请指出实际的实现。 我在看android源码 ContextWrapper.java 包含 sendBroadcast(
我没遇到什么问题。我正在刷新我的 SD 卡: sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + E
我又遇到了一个与 Android 相关的问题。这只是为了让我更深入地了解发送广播的工作原理。 我想知道的是: 1。为什么要在 Activity 上使用广播接收器来获取结果? 我认为广播接收器对于处理实
在广播接收器中发送广播有什么问题吗? 最佳答案 我认为从 BroadcastReceiver 中再次发送广播没有任何问题。 你可以看到类似的例子here. 要了解有关广播接收器的更多信息,请参阅 th
我有一个创建线程的服务。该线程做了一些工作,然后在 Service 上调用 sendBroadcast()。这意味着 sendBroadcast() 是从工作线程调用的,而不是从运行服务的线程调用的。
我有两个应用程序,App A 和 App B。 App A 需要向 App B 发送一些数据(或进行通信以做某事)。 最好的方法是什么。App B 可以有一个广播监听器来接收来自 App A 的广播。
startActivity() 和 sendBroadcast() 有什么区别?我们可以使用 startActivity() 来启动一个 Activity 。sendBroadcast() 也可以这样
我正在尝试修改作为 Android Studio 示例提供的 BluetoothLeGatt 项目,以便在每次读取特征时将 RSSI 值发送回主要 Activity 以供显示。我创建了一个新的 Int
我正在使用 IntentService 从远程源获取数据。当服务获取数据后,它使用 sendBroadcast(intent) 广播它。 在我的主要 Activity 中,我扩展了 Broadcast
LocalBroadcastManager.sendBroadcast() 的返回值( bool 值)没有记录。 我想当至少有一个接收者被注册时它返回 true,如果没有接收者被注册则返回 false
我想制作一个库,以同步方式(从客户端)执行一些 Intent 发送/接收的东西。这样我图书馆的客户就不必关心接收器的实现了。 图书馆会在某处发送 Intent 并接收响应。然后将结果同步返回给客户端。
我怎样才能摆脱编译错误? sendBroadcast(intent); 此方法在我制作的另一个应用程序中运行良好,但在当前应用程序中存在编译错误。红色下划线有错误消息“未定义新的 Runnable 类
我在服务中的线程池中有一个线程可以做很多事情。最后,它将一些数据插入提供程序并发送广播以通知 GUI 有关新数据的信息。 经常发送和接收 1-3 个广播,然后不再接收广播。当我查看线程堆栈跟踪时,它们
我有一个应用程序,它会在启动后立即发送一个广播,这样它就可以获得可用图像的刷新列表。直到今天这一切都很好(至少那是我注意到这一点的时候)。 它仍会启动,但一旦调用 Intent,焦点就会转移到主屏幕应
我是一名优秀的程序员,十分优秀!