[英]BroadcastReceiver - onReceive Not Being Called
I send a Broadcast by doing: 我通过以下方式发送广播:
Intent intent = new Intent("com.usmaan.myApp.DATA_RECEIVED");
intent.putExtra("matchId", newRowId);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
This is the Service that wraps up the AsyncTask
which runs Broadcasts the above: 这是包装运行上述广播的
AsyncTask
的服务:
<service
android:name=".services.DataService"
android:exported="false" />
In my Activity, I register a Receiver
in onResume
: 在我的Activity中,我在
onResume
注册了一个Receiver
:
IntentFilter intentFilter = new IntentFilter("com.usmaan.myApp.DATA_RECEIVED");
registerReceiver(mDataReceiver, intentFilter);
The `BroadReceiver looks like this: `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);
}
}
The onReceive
is never fired. onReceive
永远不会被解雇。 What am I doing wrong? 我究竟做错了什么?
Either use LocalBroadcastManager
in both places ( sendBroadcast()
and registerReceiver()
), or do not use LocalBroadcastManager
at all. 在两个地方使用
LocalBroadcastManager
( sendBroadcast()
和registerReceiver()
),或者根本不使用LocalBroadcastManager
。 Right now, you have a mismatched pair. 现在,你有一对不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.