BroadcastReceiver - onReceive未被调用

时间:2014-05-04 21:33:16

标签: android service broadcastreceiver intentfilter

我通过以下方式发送广播:

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永远不会被解雇。我做错了什么?

1 个答案:

答案 0 :(得分:6)

在这两个地方使用LocalBroadcastManagersendBroadcast()registerReceiver()),或者根本不使用LocalBroadcastManager。现在,你有一对不匹配。