Implement Broadcast receiver inside a Service

后端 未结 3 587
无人及你
无人及你 2020-12-18 01:33

I want to check Internet connection throughout the run time of my Android application. I tried using services but seems like it is not the best option. Is there any possible

3条回答
  •  一生所求
    2020-12-18 02:16

    It wouldn't be wise to check for the connectivity every second. Alternatively you can listen to the action (ConnectivityManager.CONNECTIVITY_ACTION) and identify if you are connected to an active network or not.

    IntentFilter filter = new IntentFilter();
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    

    Additionally you can check the network Type that is currently active(Type_WIFI, Type_MOBILE)

    This way, you don't need a service that keeps checking the connectivity every second.

提交回复
热议问题