在MVVM中检查互联网连接的最佳位置在哪里?

时间:2017-07-04 09:10:35

标签: android mvvm

我有apiClass进行所有网络通话 而且我在想我的选择是:

  1. activity / fragment

  2. 内进行检查
  3. apiClient班级

  4. 内进行检查

    我确信有更好的选择。

    修改

    This回答建议我的第二个选择。有更好的方法吗?

2 个答案:

答案 0 :(得分:2)

您可以在任何地方添加互联网连接方法,但根据我的理解,对于代码/方法方法重用,您可以创建Util类,也可以将其添加到Application类中。 您也可以参考以下方法。

public static boolean getConnectionStatus(Context context) {
    ConnectivityManager mConnectivityManager;
    NetworkInfo mNetworkInfoMobile, mNetworkInfoWifi;

    mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    mNetworkInfoMobile = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    mNetworkInfoWifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    try {
        if (mNetworkInfoMobile.isConnected()) {
            App.connectivityStatus = 1;

            return true;
        }
    } catch (Exception exception) {
        // exception.printStackTrace();
    }

    if (mNetworkInfoWifi.isConnected()) {
        App.connectivityStatus = 1;

        return true;
    } else {
        App.connectivityStatus = 0;
        return false;
    }
}

答案 1 :(得分:0)

在实时数据中创建类连接管理器

class Connectivity_check_internet(val context: Context) : LiveData<Boolean>() {
    val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    var Connnectserver: Boolean? = false
    val reciveconnctivitymanager = object : BroadcastReceiver() {
        override fun onReceive(p0: Context?, p1: Intent?) {
            Connnectserver=false
            UnpdateConnection()
            postValue(Connnectserver)
        }
    }
    init {
        UnpdateConnection()
    }

    fun UnpdateConnection(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
            capabilities?.run {
                Connnectserver=capabilities!!.hasTransport(TRANSPORT_CELLULAR) || capabilities.hasTransport(TRANSPORT_WIFI)
            }
        } else {
            try {
                val activeNetworkInfo = connectivityManager.activeNetworkInfo
                Connnectserver = activeNetworkInfo != null && activeNetworkInfo.isConnected
            } catch (e: Exception) {
            }
        }
    }

    override fun onActive() {
        super.onActive()
        context.registerReceiver(
            reciveconnctivitymanager,
            IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
        )
    }

    override fun onInactive() {
        super.onInactive()
        context.unregisterReceiver(reciveconnctivitymanager)
    }
}

类 MainActivity : AppCompatActivity() { val Connectivity_internet by 懒惰{ Connectivity_check_internet(this) }

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    Connectivity_internet.observe(this,{
        Log.e("TAG", "onCreate: "+it )
    })

}

}