It is not complex to Check Android Network / Internet Connectivity Status, bellow DetectConnection class
will help you to check Android Network / Internet Connectivity Status. Here I am going to load Android Wireless & Network Setting if device is not connected to any Network.
- Create an Android project called CheckConnection , visit my previous post First Android Application in Eclipse Hello world Android Example
CheckConnectionActivity Activity will load Wireless & Network Setting if your Android device is not connected to the Network.
package com.jsupport; import android.os.Bundle; import android.provider.Settings; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.app.Activity; import android.content.Intent; public class CheckConnectionActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.check_connection); Button check_connection = (Button) findViewById(R.id.btn_checkConnection); check_connection.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (DetectConnection .checkInternetConnection(CheckConnectionActivity.this)) { Toast.makeText(CheckConnectionActivity.this, "You have Internet Connection", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(CheckConnectionActivity.this, "You Do not have Internet Connection", Toast.LENGTH_LONG).show(); CheckConnectionActivity.this.startActivity(new Intent( Settings.ACTION_WIRELESS_SETTINGS)); } } }); } }
Create DetectConnection class
Context.getSystemService(Context.CONNECTIVITY_SERVICE)which will gives the Network information, network availability and the connectivity status of the device
package com.jsupport; package com.jsupport; import android.content.Context; import android.net.ConnectivityManager; public class DetectConnection { public static boolean checkInternetConnection(Context context) { ConnectivityManager con_manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (con_manager.getActiveNetworkInfo() != null && con_manager.getActiveNetworkInfo().isAvailable() && con_manager.getActiveNetworkInfo().isConnected()) { return true; } else { return false; } } }
Add following permission to your AndroidManifest.xml to allow internet connection and to access Network Settings
< uses-permission android:name="android.permission.INTERNET" /> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6 comments:
Hi ,
Great blog! Is there an email address I can contact you in private?
Hi Nikos
I'm on google plus
MERECE O COMENTÁRIO DE UM BRASILEIRO.... "SHOW DE BOLA" ESTE TUTORIAL, PARABÉNS!... SIMPLES, DETALHADO, E PONTUAL!
Obrigado Achutti
Thankx :)
Thanx...its so simple..
Post a Comment