Home > Back-end >  How to check internet with LiveData in Android
How to check internet with LiveData in Android

Time:09-02

In my application I want check internet connection and for this I want use LiveData.
I write below codes, but after build project show me error!
I used Hilt for dependency injection.

CheckConnection class :

class CheckConnection @Inject constructor(private val cm: ConnectivityManager) : LiveData<Boolean>() {

    constructor(application: Application) : this(
        application.getSystemService(Context.CONNECTIVITY_SERVICE)
                as ConnectivityManager
    )

    private val networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            postValue(true)
        }

        override fun onLost(network: Network) {
            super.onLost(network)
            postValue(false)
        }
    }

    override fun onActive() {
        super.onActive()
        val request = NetworkRequest.Builder()
        cm.registerNetworkCallback(request.build(), networkCallback)
    }

    override fun onInactive() {
        super.onInactive()
        cm.unregisterNetworkCallback(networkCallback)
    }

}

With below codes I initialize this in Fragment.

@Inject
lateinit var connection : CheckConnection

After build project show me below error :

D:\Projects\Android\MyTest\KotlinArchitectureExamples\mvvm\build\generated\hilt\component_sources\debug\com\app\mvvm\MyApp_HiltComponents.java:135: error: [Dagger/MissingBinding] android.net.ConnectivityManager cannot be provided without an @Inject constructor or an @Provides-annotated method.
  public abstract static class SingletonC implements MyApp_GeneratedInjector,
                         ^
      android.net.ConnectivityManager is injected at
          com\app.mvvm.food.utils.CheckConnection(cm, �)
      com\app.mvvm.food.utils.CheckConnection is injected at
          com\app.mvvm.food.ui.list.FoodsListFragment.connection
      com\app.mvvm.food.ui.list.FoodsListFragment is injected at
          com\app.mvvm.food.ui.list.FoodsListFragment_GeneratedInjector.injectFoodsListFragment(com\app.mvvm.food.ui.list.FoodsListFragment) [com\app.mvvm.MyApp_HiltComponents.SingletonC ? com\app.mvvm.MyApp_HiltComponents.ActivityRetainedC ? com\app.mvvm.MyApp_HiltComponents.ActivityC ? com\app.mvvm.MyApp_HiltComponents.FragmentC]

How can I fix it?

CodePudding user response:

The error message says:

[Dagger/MissingBinding] android.net.ConnectivityManager cannot be provided without an @Inject constructor or an @Provides-annotated method.

It means that Dagger doesn't know to get an instance of ConnectivityManager, which is required in your CheckConnection.

Since you don't own ConnectivityManager, you need to provide it through @Provides. So in your Dagger/Hilt module you should add something like:

@Provides
fun provideConnectivityManager(@ApplicationContext context: Context) =
    context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

This way ConnectivityManager is added to the dependency graph and can be injected where it's requested.

  • Related