Why LocalBroadcastManager is not working in service?

有些话、适合烂在心里 提交于 2019-12-07 08:00:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!