I have used the code to to play mp3 sound after 30 secs interval and 30 mts distance but it does not play after the mobile is sleeping and I have travelled well over 5 miles.
HOW to make the code above work ?
Please Help.
Thanks in Advance.
I had an addon query : Does onLocationChanged method know my last location? In other words if I have executed this statement :
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 30000, new MyLocationListener());
Will mp3 file execute every 30 secs interval and 30 mts distance by itself? OR will I have to execute the statement within ScheduledExecutorService like this:
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleWithFixedDelay(new Runnable(){
public void run() {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 30000, new MyLocationListener());
}
}, 0, 600, TimeUnit.SECONDS);
Total Code :
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30, 30000, new MyLocationListener());
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
///Audio file which plays everytime the above condition is met
mp3.start();
);
Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
System.out.println("==onProviderEnabled=" + s);
Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
First I think you need to check that your location parameters are what you meant. Your time of 30 actually means 30 milliseconds, I think you want 30 * 1000. Your distance is 30000 meters, which is around 18 miles. So I don’t think you wanted 30000, but 30 instead.
Now, you will only be notified according to those parameters if the location actually changes. So if in 30 seconds the location is the same or within 30 meters of the last position you won’t get notified. In addition you are using the network provider which in the best case where you have a wifi location your accuracy is 60 meters which means you may have to move quite a bit more before you get notified. If you are getting cell tower locations you may be going a long way before it switches cell towers, and hence locations.