How to check network connection such as wifi or mobile networks to determine approximate location in android.
Asked
Active
Viewed 197 times
1 Answers
0
I didn't understood what you are asking really.
1) Are you asking how to test if you are connected to a network or if is there a network available ?
`public boolean isNetworkOnline() {
boolean status=false;
try{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getNetworkInfo(0);
if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
status= true;
}else {
netInfo = cm.getNetworkInfo(1);
if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
status= true;
}
}catch(Exception e){
e.printStackTrace();
return false;
}
return status;
}
2) Do you want to get the location of your device ?
2.1) Why don't you get the location through gps which is more accurate ?
To get the location of your device:
a) create a location manager object: private LocationManager lm;
lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
b) Request location updates from what providers do you want:
-> GPS: lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
-> NETWORK: lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

Hitman
- 588
- 4
- 10
-
i want to know about the status of network in location service (setting), which is used for determine approximate location from sources such as wifi and mobile. – Vishwajeet Nov 25 '13 at 09:45
-
So you want to know if the "Use wireless networks" is checked in the Location services from Settings ? If is that so, check this link: http://stackoverflow.com/questions/10311834/android-dev-how-to-check-if-location-services-are-enabled – Hitman Nov 25 '13 at 09:54
-
Thanks user42949,i got solution. – Vishwajeet Nov 25 '13 at 13:37