Search code examples
androidandroid-intentbroadcastreceiveradbbroadcast

adb shell broadcast not work while sendBroadcast(intent) works well


I have a class registered receiver like below:

onStart() {
    IntentFilter filter = new IntentFilter();
    filter.addAction("ActionName");
    LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
}

onStop() {
    localBroadcastManager.unregisterReceiver(receiver);
}

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(LOG_TAG, String.format("Broadcast received [action=%s]", intent.getAction()));
    }
}

In some other classes, I use code to broadcast Intent. It works perfect.

Intent intent = new Intent("ActionName");
intent.putExtra("Extra", someObject);
LocalBroadcastManager.getInstance(service).sendBroadcast(intent);

However, I try to use adb shell to trigger this function. It never responses.

adb shell am broadcast -a ActionName -e Extra someObject

Do I use add command correctly?


Solution

  • The LocalBroadcastManager is a helper to register for and send broadcasts of Intents to local objects within your process. This has a number limitation compared to Contenxt.sendBroadcast(), one of them is that it is not possible for other applications to send these broadcasts to your app.