为什么LocalBroadcastManager不在服务中?

时间:2015-09-17 09:37:29

标签: android android-broadcastreceiver

我使用了服务,我没有收到任何广播的消息。  需要快速回复。

  

这是我使用的意图过滤字符串。

public class AppConstant {

   public static final String FILTER = "com.sample.hmi.REQUEST_PROCESSED" 
   .....
}
  

我的服务看起来像这样

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);
    }
.....

}
  

在我的活动中

    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) {

      .....

     }
    }

任何人都可以帮助我。需要快速回复。

0 个答案:

没有答案