我有一个Service
,我将广播intent
发送到BroadcastReceiver
,然后此广播接收器将其发送回{{1}内指定的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
中的BroadcastReceiver
:
Service
这是 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"/>
即使两者都有相同的广播ID MyBroadcastReceiver
,也没有收到任何广播。
为什么它没有收到R.string.broadcast_id
发送的广播?
请告诉我。
答案 0 :(得分:0)
此处的问题是在注册OUTER JOIN
时未添加第myIntentFilter.addAction(getString(R.string.broadcast_id));
行。
添加线后解决了。
感谢Jitesh Mohite和Hasif Seyd帮助我找到这个小而重要的错误。