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 way for me to implement a broadcast receiver with in a service? Or do I have to give up the service and use broadcast receivers alone?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
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.
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.
I now show how to create SMS receiver in a service:
I hope this link will be helpful. Just go through it.
You need not to use
Service
orBroadCastReceiver
for this purpose. Just check Connection status everyTime you need to ping the server.you can write a method which checks this and returns a
boolean
(true/false) according to connection status. Below method does the same.