4

我使用了服务,但没有收到任何广播消息。需要快速响应。

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

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

      .....

     }
    }

任何人都可以帮我解决这个问题。需要快速响应。

4

0 回答 0