Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check internet with LiveData in Android

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading