11

I've got this code:

private AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
private PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, new Intent("my action-name"), 0);

alarmManager.setInexactRepeating((int)AlarmManager.ELAPSED_REALTIME_WAKEUP,     SystemClock.elapsedRealtime() + autoUpdateIntervalInMinutes * 60 * 1000,  autoUpdateIntervalInMinutes * 60 * 1000, alarmIntent);

But I would like to change this for LocalBroadcastManager. Is this possible?

Ahmed Alnabhan
  • 608
  • 1
  • 9
  • 13
Bartłomiej Mucha
  • 2,762
  • 1
  • 30
  • 36

2 Answers2

12

No, it is not possible, because LocalBroadcastManager is only for your own process, and AlarmManager's backend runs in a different process. That is why there is no way to create a PendingIntent that works with LocalBroadcastManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    is there a way to make the intent more private , so that other apps won't be able to use it? – android developer Dec 26 '13 at 15:30
  • 2
    In short, no. However any other apps that wish to listen to your broadcasts would need to know the full action string. This at least gives you some security through obscurity (which isn't really security). – Doge Jul 04 '14 at 14:46
4

But you could register a BroadcastReceiver which basically converts the "Global" Broadcast into a LocalBroadcast:

public class AutoUpdateBroadcastReceiver extends BroadcastReceiver {

  private static final String TAG = AutoUpdateBroadcastReceiver.class.getSimpleName();

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(TAG, ".onReceive");
    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
    lbm.sendBroadcast(intent);
  }
}
deveth0
  • 49
  • 1
  • 2
    Yes, you _can_ but that doesn't mean you _should_. The whole point of the `LocalBroadcastManager` is that it can only receive broadcasts from within the application, for security reasons. If you create a receiver that receives broadcasts from the outside and re-broadcasts them on the local manager, it defeats the whole purpose of `LocalBroadcastManager`. You might as well just register the receiver non-locally and not endanger everything else that might be registered on the `LocalBroadcastManager`. – spaaarky21 May 02 '16 at 19:07