Question

Can anyone tell me a good approach to communicate from an Android Library Project to the App which uses this Library?

A little description: my library receives GCM notifications and forwards some of them to the App using this library. Right now I realized this with Intents send by the library and a BroadcastReceiver listening for that intent in the App.

The problem: when I install 2 Apps with my application, both receive each others notifications. Anybody an idea?

Thanks in advance!

[Edit]

Here is some Code. I receive a GCM Notification in the library and forward it to the consuming App:

GCMIntentService:

@Override
protected void onHandleIntent(Intent intent) {
   ...
        String notificationString = intent
            .getStringExtra(GCMConstants.NOTIFICATION);

        Intent broadIntent = new Intent(getResources().getString(
                R.string.con_broadcast_gcm_notification));
        broadIntent.putExtra("callback", notification.getCallback());
        context.sendBroadcast(broadIntent);
    ...
    }

and my BroadcastReceiver listens for con_broadcast_gcm_notification. It is registered in the manifest via Intent-Filter.

manifest.xml

    ...
    <receiver android:name=".MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="de.tuberlin.snet.gcm.notification" />
        </intent-filter>
    </receiver>
    ...
Was it helpful?

Solution 2

The correct way to do this in Android depends on how your 'library' is installed.

If your library is installed as a separate 'application' in its own right, the solution is to use different intent filters for distinct broadcasts. In that way Android will deliver a broadcast only to those apps which have advertised their interest. You'll need to update your client apps with distinct intent filters, and change your library to use the intent filter for the appropriate client as part of its broadcast.

If your library is bundled with both of your clients, then using a LocalBroadcastManager approach is the route to take.

OTHER TIPS

You can use LocalBroadcasts instead of normal broadcasts. They essentially are like real broadcasts, but only visible to one application. I assume you want to communicate from a Service to the app? Then LocalBroadcasts should be exactly what you are looking for, but without knowing exactly how you implemented anything I cannot give you very specific advice.

Anyway if you want to use LocalBroadcasts you first have to create a BroadcastReceiver like you would with a normal broadcast:

private static final String SOME_ACTION = "someAction";

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(SOME_ACTION.equals(action)) {
            // Do your work
        }
    }
};

You can then register and unregister the BroadcastReceiver like this:

@Override
public void onResume() {
    super.onResume();

    IntentFilter intentFilter = new IntentFilter(SOME_ACTION);

    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
    manager.registerReceiver(broadcastReceiver, intentFilter);
}

@Override
public void onPause() {
    super.onPause();

    LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
    manager.unregisterReceiver(broadcastReceiver);
}

And finally you can send a broadcast from your Service or anywhere else in your application like this:

Intent intent = new Intent(SOME_ACTION);

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.sendBroadcast(intent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top