简体   繁体   English

从Android库项目到使用它的App的独特通信

[英]Distinct communication from Android Library Project to App using it

Can anyone tell me a good approach to communicate from an Android Library Project to the App which uses this Library? 谁能告诉我从Android库项目到使用该库的应用进行通信的好方法吗?

A little description: my library receives GCM notifications and forwards some of them to the App using this library. 简要说明:我的图书馆收到GCM通知,并使用此图书馆将其中的一些转发给App。 Right now I realized this with Intents send by the library and a BroadcastReceiver listening for that intent in the App. 现在,我通过库发送的Intents和在App中监听该Intent的BroadcastReceiver意识到了这一点。

The problem: when I install 2 Apps with my application, both receive each others notifications. 问题是:当我在自己的应用程序中安装2个Apps时,两者都会收到彼此的通知。 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: 我在库中收到GCM通知,并将其转发到使用中的应用程序:

GCMIntentService: 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. 我的BroadcastReceiver会监听con_broadcast_gcm_notification。 It is registered in the manifest via Intent-Filter. 它通过Intent-Filter注册在清单中。

manifest.xml manifest.xml

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

You can use LocalBroadcasts instead of normal broadcasts. 您可以使用LocalBroadcasts代替普通广播。 They essentially are like real broadcasts, but only visible to one application. 它们本质上就像真实的广播,但仅对一个应用程序可见。 I assume you want to communicate from a Service to the app? 我假设您想从Service与应用进行通信? 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. 然后, LocalBroadcasts应该正是您要寻找的东西,但是在不确切知道如何实现任何事情的情况下,我无法为您提供非常具体的建议。

Anyway if you want to use LocalBroadcasts you first have to create a BroadcastReceiver like you would with a normal broadcast: 无论如何,如果您想使用LocalBroadcasts ,则首先必须创建一个BroadcastReceiver就像使用普通广播一样:

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: 然后,您可以像这样注册和取消注册BroadcastReceiver

@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: 最后,您可以从Service或应用程序中的其他任何地方发送广播,如下所示:

Intent intent = new Intent(SOME_ACTION);

LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.sendBroadcast(intent);

The correct way to do this in Android depends on how your 'library' is installed. 在Android中执行此操作的正确方法取决于您的“库”的安装方式。

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. 这样,Android只会将广播发送给那些已经宣传了自己兴趣的应用。 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. 您需要使用不同的Intent过滤器更新客户端应用程序,并更改库以将适当的客户端使用Intent过滤器作为其广播的一部分。

If your library is bundled with both of your clients, then using a LocalBroadcastManager approach is the route to take. 如果您的库与两个客户端捆绑在一起,则采用LocalBroadcastManager方法是可以采取的途径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM