4

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.

  • Where starting `MyService` Service? – ρяσѕρєя K Sep 17 '15 at 09:40
  • Have you defined the intent filter in the manifest as well? – Frank D. Sep 17 '15 at 09:48
  • i have already checked if service is running and its working great. Only the Localbroadcast is not working. Right now i am using sendBroadcast(intent); instead of LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent); and registerReceiver(mMessageReceiver, new IntentFilter(AppConstant.FILTER)); in onresume of activity – Abdulla K S Sep 17 '15 at 12:23
  • glad to know i'm not the only one facing this issue – daisura99 Apr 26 '19 at 08:18

0 Answers0