LocalBroadcastManager not working as expected

2k views Asked by At

In my Application I have to notify my Activity from IntentService class . For that purpose I am using LocalBroadcastManager. But I am not receiving anything in the onReceive of my Broadcast Receiver. Here is what I have written.

In my BaseActivity I have registered my receiver.

public class BaseActivity extends FragmentActivity implements App {

@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}

@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(
            mMessageReceiver, new IntentFilter(custom-event-name));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(
            mMessageReceiver);
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        System.out.println("Overlay Message" +bundle.getString("message"));
          }
      };
  }

I am sending a local broadcast from my RegisterAlarmIntentService class.

  public class RegisterAlramIntentService extends WakefulIntentService implements
           APIConstants {

    public RegisterAlramIntentService() {
        super("AlarmService");
   }

    @Override
   public String getTag() {
        return "AlarmService";
   }

@Override
protected void onHandleIntent(Intent intent) {
            System.out.println("Working till here fine In RegisterAlarm");
            Bundle bundle = intent.getExtras();
            Intent localIntent = new Intent(custom-event-name);
            localIntent.putExtras(bundle );
            LocalBroadcastManager.getInstance(this).sendBroadcast(
                        localIntent);


      }
  }

onHandleIntent() method is called. But nothing is received in onReceive() of my receiver. Please Help. Thanks in advance!!

1

There are 1 answers

1
Chirag Jain On BEST ANSWER

Try

 public class RegisterAlramIntentService extends WakefulIntentService implements
           APIConstants {

   Intent localIntent;

    public RegisterAlramIntentService() {
        super("AlarmService");

       localIntent = new Intent(custom-event-name);
   }

    @Override
   public String getTag() {
        return "AlarmService";
   }

@Override
protected void onHandleIntent(Intent intent) {
            System.out.println("Working till here fine In RegisterAlarm");
            Bundle bundle = intent.getExtras();
            Thread.sleep(5000); // For Testing only because it is in whole new thread (which may not wait for your reciever to setup)
            localIntent.putExtras(bundle );
            LocalBroadcastManager.getInstance(this).sendBroadcast(
                        localIntent);


      }
  }

Also in manifest :

<service android:name="com.commonsware.android.localcast.RegisterAlramIntentService"/>

See this