Wednesday, December 8, 2010

How To Check Android Wifi And 3G Network Status

Here is the way to check your Android Wifi and 3G connectivity network status which is connected to your Phone by using a code to check the current status of your Android network status and using the permission:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

import android.net.ConnectivityManager;
import android.os.Bundle;
import android.widget.Toast;
public class pingtest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

chkStatus();

}

void chkStatus()
{
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);

final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
}

}


How To Check Android Wifi And 3G Connect Status

2 comments:

Hello, thank you for this code. I'm not a experienced android programmer, I'm doing a not big project for android and im using this code, but when I put in on Eclipse, on the 2.2 android SDK, it shows me an error on the permissions. It says: "Syntax error on tokens". what could it be?

Post a Comment