【发布时间】:2017-02-01 05:01:12
【问题描述】:
我有一个接收网络连接事件的 BroadcastReceiver 实现。它在 AndroidManifest.xml 中声明,当网络事件发生时由 Android 自动调用。
广播接收者:
public class ConnectivityChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "action: " + intent.getAction());
Log.v(TAG, "component: " + intent.getComponent());
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
<receiver
android:name=".ConnectivityChangeReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
我想为我的应用使用此处描述的 Google MVP 示例架构:
https://github.com/googlesamples/android-architecture/tree/todo-mvp/
使用上面的架构,只是想知道:
我的 BroadcastReceiver 应该放在哪里?
如果我的 BroadcastReceiver 需要写入数据库,最好的方法是什么?
如果我的 BroadcastReceiver 需要更新 UI,最好的方法是什么?
【问题讨论】:
-
使用 EventBus 怎么样? github.com/greenrobot/EventBus
-
@Rory 一个意图服务怎么样,它需要在您的应用程序流程中显式启动和停止?
-
@ChintanSoni 大泥巴怎么办?! ;)) 他试图通过添加“组件”来创建一些架构,而不是快速和肮脏地解决它,该组件将与每个人交谈并倾听每个人,而无需任何义务或合同。
-
@Ewoks 希望改变观念对您有所帮助;)
-
只是想知道 EventBus 属于 MVP(或其他架构)的哪个部分......考虑它改变了我的观点