在我的服务中,我使用LocalBroadcastManager向活动发送信息,但活动从未收到任何内容。
在我的服务中
Intent intent = new Intent("openJobsBroadcast");
Log.d(TAG, "Broadcasting open jobs count");
// You can also include some extra data.
intent.putExtra("openJobs", 5);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
在活动中,我在onCreate
中注册了收藏家@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_activity);
transfer = (TextView) findViewById(R.id.transfer);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("openJobsBroadcast"));
}
使用此接收器
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String openJobsCount = intent.getStringExtra("openJobs");
Log.d(TAG, "received new openJobCount from service with value " + openJobsCount);
}
};
服务在清单中定义,如
<service
android:name="android.uploader.services.AttachService"
android:icon="@drawable/loading_icon"
/>
知道为什么我的活动中的onReceive永远不会被调用?
由于