Search This Blog

Monday 25 November 2013

How to check the INTERNET & GPS connection in Android


----------------------------------------------------------------------------------------------------
ConnectivityManager only give information of the connection (WiFi, mobile, WiMax, etc) and if it is connected or not.

public boolean isGPSEnabled(){

LocationManager lm;
boolean GPS_Status =false;
boolean gps_enabled = false;
String title,message="";
try{
lm = (LocationManager)this.getSystemService(LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception e){
e.printStackTrace();
}

if(gps_enabled){
GPS_Status = true;
title="GPS ENABLED";
message ="GPS is  enabled.";
GPSChecker.showAlertDialog(this, title, message);
}else{
title="GPS Disabled";
message ="GPS is not enabled.Please TurnOn the GPS";
GPSChecker.showAlertDialog(this, title, message);
}
return GPS_Status;
}

----------------------------------------------------------------------------------------------------


/*
* This method does check-  ARE WE CONNECTED TO THE NET
*/
public final boolean isInternetOn()
{
String title,message="";
boolean connected = false;
ConnectivityManager connec = (ConnectivityManager)
   getSystemService(Context.CONNECTIVITY_SERVICE);

 if (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
 connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ){

 // MESSAGE TO SCREEN FOR TESTING (IF REQ)
 title="Internet ENABLED";
 message ="Internet is  enabled.";
 GPSChecker.showAlertDialog(this, title, message);
 connected = true;

 }else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
 ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ){

 connected =  false;
 title="Internet DISABLED";
 message ="Internet is  disabled.";
 GPSChecker.showAlertDialog(this, title, message);
 }
 return connected;
}


----------------------------------------------------------------------------------------------------

No comments:

Post a Comment