来自谷歌的示例源代码很容易在前端实现连续的位置更新,但我仍然可以使用FusedLocationApi和PendingIntent清楚地了解背景位置更新是如何工作的.
LocationService类:
public class LocationService extends IntentService {
private static final String TAG = LocationService.class.getSimpleName();
private static final String ACTION_LOCATION_UPDATED = "location_updated";
private static final String ACTION_REQUEST_LOCATION = "request_location";
public static IntentFilter getLocationUpdatedIntentFilter() {
return new IntentFilter(LocationService.ACTION_LOCATION_UPDATED);
}
public static void requestLocation(Context context) {
Intent intent = new Intent(context, LocationService.class);
intent.setAction(LocationService.ACTION_REQUEST_LOCATION);
context.startService(intent);
}
public LocationService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent != null ? intent.getAction() : null;
if (ACTION_REQUEST_LOCATION.equals(action)) {
onRequestLocation();
}
else …Run Code Online (Sandbox Code Playgroud)