谁能告诉我一个很好的方法从Android库项目到使用这个库的应用程序进行通信?
一点描述:我的库收到GCM通知,并使用此库将其中一些转发到应用程序.现在我通过库发送的Intents和在App中监听该意图的BroadcastReceiver实现了这一点.
问题:当我使用我的应用程序安装2个应用程序时,都会收到彼此通知.有人有想法吗?
提前致谢!
[编辑]
这是一些代码.我在库中收到GCM通知并将其转发到正在使用的应用程序:
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);
...
}
Run Code Online (Sandbox Code Playgroud)
我的BroadcastReceiver侦听con_broadcast_gcm_notification.它通过Intent-Filter在清单中注册.
的manifest.xml
...
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="de.tuberlin.snet.gcm.notification" />
</intent-filter>
</receiver>
...
Run Code Online (Sandbox Code Playgroud) 在Java中使用RxJava2时,我可以map()自动过滤发出的空值.但是在Kotlin中有可空类型我最终会做这样的事情:
val loadConferencesSingle = service.getConferences()
.map { it.conferences ?: listOf<Conference>() }
Run Code Online (Sandbox Code Playgroud)
service.getConferences()在这种情况下的呼叫是单个发射ConferecesResponse看起来像
data class ConferencesResponse(val conferences: List<Conference?>? = null)
Run Code Online (Sandbox Code Playgroud)
因此,如果完整发布的会议列表为空,我最终使用Elvis运算符来发出一个空列表.map无法发送空值.
有没有人知道如何使用Kotlin在RxJava中更好地处理可空类型?我已经喜欢这个解决方案及其简洁,但我觉得有更好的方法可以解决这个问题.