it there an Android event lister for speed?
I have an app for speed measuring and would to detect when a user goes above or below certa开发者_运维百科in speed. Do I have to constantly monitor the speed or there an event I can create?
You need to monitor for location changes, and when you get an alarm of it, you need to check if the current location hasSpeed()
/**
*
*/
private void requestUpdates() {
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// loop through all providers, and register them to send updates
List<String> providers = locMan.getProviders(false);
for (String provider : providers) {
Log.e("birthdroid", "registering provider " + provider);
long minTimeMs = 5 * 60 * 1000;// 5 minute interval
float minDistance = Application.LOCATION_HOT_RADIUS_IN_METERS;
locMan.requestLocationUpdates(provider, minTimeMs, minDistance,
getIntent());
}
}
/**
* convenient method to get pending intent
* @return
*/
private PendingIntent getIntent() {
Intent intent = new Intent(this, LocationReceiver.class);
return PendingIntent.getBroadcast(
getApplicationContext(), 0, intent, 0);
}
and the Receiver can be
public class LocationReceiver extends BroadcastReceiver {
/*
* (non-Javadoc)
*
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle b = intent.getExtras();
Location loc = (Location) b
.get(android.location.LocationManager.KEY_LOCATION_CHANGED);
if (loc != null) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
There is no event for speed, you'd have to calculate it and sound an "alarm" whenever the speed is above the threshold.
精彩评论