Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service to check internet connectivity?

Tags:

android

I want to create an Android service, which notifies the main activity whenever disconnects and when internet reconnects again. I have following function for checking internet connectivity:.

private boolean haveInternet(){
        NetworkInfo info=(NetworkInfo)((ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if(info==null || !info.isConnected()){
            return false;
        }
        if(info.isRoaming()){
            //here is the roaming option you can change it if you want to disable internet while roaming, just return false
            return true;
        }
        return true;
    }

But I want to know, how to use it in a service.

like image 667
Adnan Avatar asked Jun 29 '10 14:06

Adnan


People also ask

How do I check my internet connectivity?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top.

What is internet service in Android?

Android lets your application connect to the internet or any other local network and allows you to perform network operations. A device can have various types of network connections. This chapter focuses on using either a Wi-Fi or a mobile network connection.

How do I check programmatically for internet connection?

Check Internet Connection Status In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object. Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.


1 Answers

Services are designed for long backgroud running task. You should use a BroadcastReceiver:

This is a sample method I use to monitor the network state into my main Activity:

private void installListener() {

        if (broadcastReceiver == null) {

            broadcastReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {

                    Bundle extras = intent.getExtras();

                    NetworkInfo info = (NetworkInfo) extras
                            .getParcelable("networkInfo");

                    State state = info.getState();
                    Log.d("InternalBroadcastReceiver", info.toString() + " "
                            + state.toString());

                    if (state == State.CONNECTED) {

                        onNetworkUp();

                    } else {

                        onNetworkDown();

                    }

                }
            };

            final IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            registerReceiver(broadcastReceiver, intentFilter);
        }
    }

Remember to call unregisterReceiver when the onDestroy event occurs

Hope this help.

like image 191
Francesco Laurita Avatar answered Oct 18 '22 09:10

Francesco Laurita