我通过以下方式发送广播:
Intent intent = new Intent("com.usmaan.myApp.DATA_RECEIVED");
intent.putExtra("matchId", newRowId);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
这是包装运行上述广播的AsyncTask
的服务:
<service
android:name=".services.DataService"
android:exported="false" />
在我的活动中,我在Receiver
注册了onResume
:
IntentFilter intentFilter = new IntentFilter("com.usmaan.myApp.DATA_RECEIVED");
registerReceiver(mDataReceiver, intentFilter);
`BroadReceiver看起来像这样:
private class DataReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final long matchId = intent.getLongExtra("matchId", 0);
Toast.makeText(LaunchActivity.this, "" + matchId, Toast.LENGTH_LONG);
}
}
onReceive
永远不会被解雇。我做错了什么?
答案 0 :(得分:6)
在这两个地方使用LocalBroadcastManager
(sendBroadcast()
和registerReceiver()
),或者根本不使用LocalBroadcastManager
。现在,你有一对不匹配。