问题
I used Service and i am not recieving any broadcasted message. Need quick response.
This is the intent filter string i used.
public class AppConstant {
public static final String FILTER = "com.sample.hmi.REQUEST_PROCESSED"
.....
}
My service look like this
public class MyService extends Service {
.....
{
.....
broadcastResponse(true);//bradcastcall
....
}
private void broadcastResponse(boolean isTrue) {
Intent intent = new Intent(AppConstant.FILTER);
intent.putExtra(AppConstant.COMMAND, AppConstant.HMI_BUTTON_RESPONSE);
intent.putExtra(AppConstant.DATA_IS_TRUE, isTrue);
LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent);
}
.....
}
In my activity
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
LocalBroadcastManager.getInstance(MyApplication.getContext()).registerReceiver(mMessageReceiver, new IntentFilter(AppConstant.FILTER));
....
}
@Override
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(MyApplication.getContext()).registerReceiver((mMessageReceiver),
new IntentFilter(AppConstant.FILTER));
}
@Override
protected void onStop() {
LocalBroadcastManager.getInstance(MyApplication.getContext()).unregisterReceiver(mMessageReceiver);
super.onStop();
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("KBR", "Got message");
handleMessage(intent);
}
};
private void handleMessage(Intent msg) {
.....
}
}
Can anybody help me with this. Need quick response.
来源:https://stackoverflow.com/questions/32627063/why-localbroadcastmanager-is-not-working-in-service