I am making an android application to show the position of a user on a map, along with the users current longitude and latitude, I have also used reverse geocoding to show the address of the android device. Now I plan to add a feature that if the user is near a particular place the phone automatically switches to silent mode, for this I have made a function to check whether the phone is already silent or not. Now I want to make another one, which puts the phone in silent mode on nearing a particular location.
The method which I have made to make the phone go silent on nearing a particular location is
public void changeToSilent(Location location){
if(distanceTo(xxxx)<100)
{
if(mPhoneIsSilent==true){
}
else
{
mPhoneIsSilent = true;
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
}
Now I am calling this method in another function, which is….
public void onLocationChanged(Location location)
{
Log.d(TAG, “onLocationChanged with location ” + location.toString()); Stringtext=String.format(“Lat:\t%f\nLong:\t%f\nAlt:\t%f\nBearing:\t%f”,location.getLatitude(),location.getLongitude(), location.getAltitude(), location.getBearing());
this.locationText.setText(text);
changeToSilent(location);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point);
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
Now I am calling the above function in the oncreate method. Now suppose I know the latitude and longitude of the place near which the phone should go to silent.
What I want to know is, what do I have to write in place of xxxx in the changetosilent method?
You can use the AudioManager to change the system volume levels.
here is an example:
EDIT: Get a Location object and use loc.distanceBetween();