BroadcastReceiver 的 onReceive() 接收不到另一个 BroadcastReceiver 发送的广播
BroadcastReceiver's onReceive() is failing to receive the broadcast sent by another BroadcastReceiver
我有一个 Service
,我从那里发送广播 intent
到 BroadcastReceiver
,然后这个广播接收器将它发送回 BroadcastReceiver
在 Service
.
代码如下:
private Notification getNotificationAU() {
mBuilder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle("title")
.setContentText(***);
// for action button
Intent actionIntent = new Intent(getBaseContext(), BroadcastSender.class);
PendingIntent actionPendingIntent = PendingIntent
.getBroadcast(getBaseContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setAutoCancel(true);
mBuilder.addAction(***, "***", actionPendingIntent);
return mBuilder.build();
}
这里是 BroadcastSender.class
代码:
public class BroadcastSender extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Received by BroadcastSender", Toast.LENGTH_SHORT).show();
// send back to your class
Intent newIntent = new Intent();
newIntent.setAction(context.getString(R.string.broadcast_id));
context.sendBroadcast(newIntent);
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();
}
}
两个上面的Toasts
都显示了。
这是 Service
中的 BroadcastReceiver
:
public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver(){
super();
}
@Override public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "onReceive() in service called", Toast.LENGTH_SHORT).show();
if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id_2))) {
...
...
...
} else if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {
// perform the task
} else {
Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
}
}
}
这是strings.xml
:
<string name="broadcast_id">***</string>
这里是AndroidManifest.xml
:
<receiver android:name=".BroadcastSender"/>
问题是 MyBroadcastReceiver
收不到任何广播,即使两者具有相同的广播 ID R.string.broadcast_id
。
为什么收不到BroadcastSender.class
发送的广播?
请告诉我。
这里的问题是在注册 BroadcastReceiver
时没有添加行 myIntentFilter.addAction(getString(R.string.broadcast_id));
。
加行后解决
感谢 Jitesh Mohite and Hasif Seyd 帮助我找出这个小而关键的错误。
我有一个 Service
,我从那里发送广播 intent
到 BroadcastReceiver
,然后这个广播接收器将它发送回 BroadcastReceiver
在 Service
.
代码如下:
private Notification getNotificationAU() {
mBuilder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle("title")
.setContentText(***);
// for action button
Intent actionIntent = new Intent(getBaseContext(), BroadcastSender.class);
PendingIntent actionPendingIntent = PendingIntent
.getBroadcast(getBaseContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setAutoCancel(true);
mBuilder.addAction(***, "***", actionPendingIntent);
return mBuilder.build();
}
这里是 BroadcastSender.class
代码:
public class BroadcastSender extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Received by BroadcastSender", Toast.LENGTH_SHORT).show();
// send back to your class
Intent newIntent = new Intent();
newIntent.setAction(context.getString(R.string.broadcast_id));
context.sendBroadcast(newIntent);
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();
}
}
两个上面的Toasts
都显示了。
这是 Service
中的 BroadcastReceiver
:
public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver(){
super();
}
@Override public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "onReceive() in service called", Toast.LENGTH_SHORT).show();
if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id_2))) {
...
...
...
} else if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {
// perform the task
} else {
Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
}
}
}
这是strings.xml
:
<string name="broadcast_id">***</string>
这里是AndroidManifest.xml
:
<receiver android:name=".BroadcastSender"/>
问题是 MyBroadcastReceiver
收不到任何广播,即使两者具有相同的广播 ID R.string.broadcast_id
。
为什么收不到BroadcastSender.class
发送的广播?
请告诉我。
这里的问题是在注册 BroadcastReceiver
时没有添加行 myIntentFilter.addAction(getString(R.string.broadcast_id));
。
加行后解决
感谢 Jitesh Mohite and Hasif Seyd 帮助我找出这个小而关键的错误。