无法将 BroadcastReceiver 与 LocalBroadcastManager 一起使用
Cannot using BroadcastReceiver with LocalBroadcastManager
我有 1 activity 和 1 服务。 服务 将在 activity 进入后台时启动,并在 [=36= 时停止] 进入前台。当服务停止时,我想按意图将一个对象从它发送到 activity。这是我的代码
MyActivity.java
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WITH_RLM)) {
//I will get the object received by service here
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_lesson_ins);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_WITH_RLM);
localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onResume() {
super.onResume();
if (intent != null) {
stopService(intent);
}
}
@Override
protected void onPause() {
super.onPause();
if (intent != null) {
startService(intent);
}
}
@Override
protected void onStop() {
super.onStop();
localBroadcastManager.unregisterReceiver(broadcastReceiver);
}
MyService.java
private RecordLessonModel recordLessonModel;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//I code sth for handle recordLessonModel object
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent intent = new Intent(MyActivity.ACTION_WITH_RLM);
intent.putExtra(MyActivity.RECORD_MODEL_NAME, Parcels.wrap(recordLessonModel));
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
这里的问题是,当我的 activity 出现在前台时,我在 MyActivity
中的 broadcastReceiver
无法接收到来自服务的 intent。不知道为什么。
谢谢。
您应该考虑将您的 broadcastReceiver 注销到 onDestory()
而不是 onStop()
因为当 activity 进入后台时您的 brodcastReceiver 正在取消注册。
更多信息请查看HERE
我有 1 activity 和 1 服务。 服务 将在 activity 进入后台时启动,并在 [=36= 时停止] 进入前台。当服务停止时,我想按意图将一个对象从它发送到 activity。这是我的代码
MyActivity.java
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WITH_RLM)) {
//I will get the object received by service here
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_lesson_ins);
localBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_WITH_RLM);
localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onResume() {
super.onResume();
if (intent != null) {
stopService(intent);
}
}
@Override
protected void onPause() {
super.onPause();
if (intent != null) {
startService(intent);
}
}
@Override
protected void onStop() {
super.onStop();
localBroadcastManager.unregisterReceiver(broadcastReceiver);
}
MyService.java
private RecordLessonModel recordLessonModel;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//I code sth for handle recordLessonModel object
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Intent intent = new Intent(MyActivity.ACTION_WITH_RLM);
intent.putExtra(MyActivity.RECORD_MODEL_NAME, Parcels.wrap(recordLessonModel));
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
这里的问题是,当我的 activity 出现在前台时,我在 MyActivity
中的 broadcastReceiver
无法接收到来自服务的 intent。不知道为什么。
谢谢。
您应该考虑将您的 broadcastReceiver 注销到 onDestory()
而不是 onStop()
因为当 activity 进入后台时您的 brodcastReceiver 正在取消注册。
更多信息请查看HERE