1

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!!

user2781627
  • 455
  • 5
  • 17
  • I am not sure if the event name can be simply a string. – user2781627 Nov 20 '14 at 05:22
  • Yes that is exactly what I have done...custom-event-name is actually a static constant I have used. – user2781627 Nov 20 '14 at 05:29
  • Yes its my base activity that I have used for extending in every other activity in my application and also, I do this work when my application is in foreground and the user can interact with it. – user2781627 Nov 20 '14 at 05:39

1 Answers1

2

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

Community
  • 1
  • 1
Chirag Jain
  • 1,612
  • 13
  • 20
  • Ok, this is a good answer, I just can't understand why works for me. I spent almost 3 hours trying to make it work. My service was executing, but my broadcast not receiving. What I did was exatcly this: Instead of instantiate my Intent inside onHandleIntent, I instantiated inside my service constructor, like your example above. And now it works. – Fuhrmann Nov 10 '16 at 02:46