我有一个用于速度测量的应用程序,可以检测用户何时超过或低于特定速度。我是否需要不断监控速度,或者可以创建一个事件?
最佳答案
您需要监视位置更改,并且在收到警报时,需要检查当前位置是否为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);
}
接收方可以是
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();
}
}
}