5

I have a main app, we call it 'app' and then sub modules called 'module1' and 'module2'. These modules are dependencies of the 'app'. The 'app' starts 'module1', which does its job and once done, it broadcasts a message, which should be picked up by the 'app' so that it could continue its flow. However, I don't seem to be successfully receiving it in the 'app'.

I am wondering, is it possible at all to send broadcast messages from submodules?

The modules are supposed to remain as much independent of the main app as possible so that could be used with other apps too, like plugins. That is why I am not adding anything in the main app's manifest related to the modules. Modules have their own manifests.

Broadcasts work fine within a module.

The code is pretty straight forward:

public class MyReceiver extends BroadcastReceiver {

mFilter = new IntentFilter("com.myapp.main.CUSTOM_INTENT");
mReceiver = new MyReceiver();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(Constants.TAG, "receiver worked");
    }

    // constructor
    public MyReceiver(){

    }
}

And in the manifest:

<receiver android:name=".MyReceiver">
        <intent-filter>
            <action android:name="com.myapp.main.CUSTOM_INTENT" />
        </intent-filter>
    </receiver>

And in the submodule:

Intent intent = new Intent("com.myapp.main.CUSTOM_INTENT");
            intent.putExtra("value", 0);
            sendBroadcast(intent);
zeeshan
  • 4,913
  • 1
  • 49
  • 58
  • "I am wondering, is it possible at all to send broadcast messages from submodules?" -- yes, though you should *really* be using `LocalBroadcastManager` or other form of in-process event bus (e.g., greenrobot's EventBus, Square's Otto), for performance, security, and simplicity. – CommonsWare Feb 12 '16 at 13:58
  • I tried LocalBoradcastManager too, from this answer, but it didn't work: http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – zeeshan Feb 12 '16 at 14:09
  • In my experiment, Context.sendBroadcast works, but LocalBroadcastManager.sendBroadcast doesn't work across modules. – Alpha Huang Feb 01 '17 at 23:27

2 Answers2

3

Just set component with full path to your receiver and package of it

Intent intent = new Intent("com.myapp.main.CUSTOM_INTENT");
intent.putExtra("value", 0);
intent.setComponent(new ComponentName(com.pkg","com.pkg.MyReceiver"));

sendBroadcast(intent);

Surprisingly, on some devices, the receiver will trigger without the setting component.

Taras Yurkiv
  • 126
  • 4
0

What about using interfaces? You could declare an interface in your module as:

public class Module1 {
private SessionManager manager;

public interface SessionManager
{
    void onTaskFinished();
}

// in the method to start Module1 something like:

public void startModule(SessionManager manager){
    this.manager = manager;
    //DO STUFF
         ...
    manager.onTaskFinished();
}

implement the interface into the class of the hosting app launching the Module1:

public class App implements SessionManager {
    void launchModule1(){
        Module1.startModule(this);
    }
    void onTaskFinished(){
        Module2.start();
    }
Federico Picci
  • 1,105
  • 10
  • 17