Activity 未收到来自服务的本地广播
Local broadcast from Service not received by Activity
我有一个 Activity
,我在本地注册了一个 BroadcastReceiver
,如下所示:
public class SomeActivity extends Activity{
public static final String PERFORM_SOME_ACTION = "PERFORM_SOME_ACTION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_activity_layout);
.....
.....
IntentFilter filter = new IntentFilter();
filter.addAction(PERFORM_SOME_ACTION);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// perform some action ...
}
};
registerReceiver(receiver, filter);
}
.....
.....
}
我有一个 Service
,我从中广播了一个 Intent
,如下所示:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent i = new Intent(SomeActivity.PERFORM_SOME_ACTION);
sendBroadcast(i); /* Send global broadcast. */
return START_STICKY;
}
这按预期工作。实现之后,我意识到本地广播更适合这种情况:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent i = new Intent(SomeActivity.PERFORM_SOME_ACTION);
LocalBroadcastManager.getInstance(this).sendBroadcast(i); /* Send local broadcast. */
return START_STICKY;
}
不幸的是,上述方案不起作用。每次都发送全局广播,而本地广播显然从来没有sent/received.
我在这里错过了什么?不能在两个不同的应用程序组件之间发送本地广播,例如两个单独的 Activity
或从 Service
到 Activity
?我做错了什么??
注:
根据 documentation,发送本地广播(intra-app 广播,其范围仅限于应用程序自己的组件)而不是全局广播(应用程序间 广播传输到每个phone) 上的单个应用程序,只要我们不需要广播在应用程序外部传播。这就是进行上述更改的原因。
What am I missing here?
使用LocalBroadcastManager
注册本地广播,目前使用Activity的registerReceiver
方法注册全局广播:
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
我有一个 Activity
,我在本地注册了一个 BroadcastReceiver
,如下所示:
public class SomeActivity extends Activity{
public static final String PERFORM_SOME_ACTION = "PERFORM_SOME_ACTION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_activity_layout);
.....
.....
IntentFilter filter = new IntentFilter();
filter.addAction(PERFORM_SOME_ACTION);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// perform some action ...
}
};
registerReceiver(receiver, filter);
}
.....
.....
}
我有一个 Service
,我从中广播了一个 Intent
,如下所示:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent i = new Intent(SomeActivity.PERFORM_SOME_ACTION);
sendBroadcast(i); /* Send global broadcast. */
return START_STICKY;
}
这按预期工作。实现之后,我意识到本地广播更适合这种情况:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent i = new Intent(SomeActivity.PERFORM_SOME_ACTION);
LocalBroadcastManager.getInstance(this).sendBroadcast(i); /* Send local broadcast. */
return START_STICKY;
}
不幸的是,上述方案不起作用。每次都发送全局广播,而本地广播显然从来没有sent/received.
我在这里错过了什么?不能在两个不同的应用程序组件之间发送本地广播,例如两个单独的 Activity
或从 Service
到 Activity
?我做错了什么??
注:
根据 documentation,发送本地广播(intra-app 广播,其范围仅限于应用程序自己的组件)而不是全局广播(应用程序间 广播传输到每个phone) 上的单个应用程序,只要我们不需要广播在应用程序外部传播。这就是进行上述更改的原因。
What am I missing here?
使用LocalBroadcastManager
注册本地广播,目前使用Activity的registerReceiver
方法注册全局广播:
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);