Skip to content Skip to sidebar Skip to footer

How To Connect My Android Application To Internet Automatically Whenever Network Found

My android application is based on network connection i.e WIFI/Mobile Network. It works fine when my mobile is connected to internet but when internet connection disconnected it st

Solution 1:

You can check the network state using broadcast receiver. Whenever the network is available, you can start your application.

First, create a background service and start your service when the device boots up. Now, in this service, register a broadcast receiver and monitor the network state. If the network is available, you can start your application; and if unavailable, you can close it.

Please refer to the code below for broadcast receiver.

publicclassBroadCastSampleActivityextendsActivity
{
    /** Called when the activity is first created. */@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
        this.registerReceiver(this.mConnReceiver, newIntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

    privateBroadcastReceivermConnReceiver=newBroadcastReceiver()
    {
        publicvoidonReceive(Context context, Intent intent)
        {
            booleannoConnectivity= intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            Stringreason= intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
            booleanisFailover= intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfocurrentNetworkInfo= (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfootherNetworkInfo= (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

            if(currentNetworkInfo.isConnected())
            {
                Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
            }
        }
    };
}

Solution 2:

I think you must need to checking continue for network connections, that means you need to check for internet connection in background tasks. Android Services is better option for that, create one Service and start it when your app starts, inside that just do one code and that is for checking Internet Connectivity, when it lost, do some task and when it found you can do whatever you want. So I suggest you to use services and get your task done.

Here are some links to refer.

http://www.tutorialspoint.com/android/android_services.htm

http://www.vogella.com/tutorials/AndroidServices/article.html

Solution 3:

I think you should create a spread thread or service in background for checking network connection after some interval . use following code in thread or service whatever you want to create .

NetworkInfo i = conMgr.getActiveNetworkInfo();
  if (i == null)
    returnfalse;
  if (!i.isConnected())
    returnfalse;
  if (!i.isAvailable())
    returnfalse;
  returntrue;

Post a Comment for "How To Connect My Android Application To Internet Automatically Whenever Network Found"