我有一个用于速度测量的应用程序,可以检测用户何时高于或低于特定速度。我是否必须不断监控速度或我可以创建的事件?
答案 0 :(得分:2)
您需要监控位置更改,当您收到警报时,您需要检查当前位置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();
}
}
}
答案 1 :(得分:0)
没有速度事件,只要速度高于阈值,你就必须计算并发出“警报”。