ich eine laufende App gemacht habe und dafür hat ihm eine Service-Klasse zu aktualisieren Benutzerposition (entfernt unwichtig Code) gemacht:Zählzeit im Hintergrund Service
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, ILocationConstants {
private static final String TAG = LocationService.class.getSimpleName();
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
private LocationData data;
@Override
public void onCreate() {
super.onCreate();
data = new LocationData();
startForeground();
}
private void startForeground() {
//BUIDLING A NOTIFICATION
startForeground(101, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
data = Parcels.unwrap(intent.getParcelableExtra(LOCATION_MESSAGE));
buildGoogleApiClient();
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
}
return START_STICKY;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
} catch (SecurityException ex) {
} catch (Exception e) {
}
}
private void sendUpdates() {
data.millisecondTime = SystemClock.uptimeMillis() - data.startTime;
data.updateTime = data.timeBuff + data.millisecondTime;
data.seconds = (int) (data.updateTime/1000);
data.minutes = data.seconds/60;
data.hours = data.minutes/60;
data.minutes = data.minutes % 60;
data.seconds = data.seconds % 60;
data.milliSeconds = (int) (data.updateTime % 1000);
Intent locationIntent = new Intent();
locationIntent.setAction(LOACTION_ACTION);
locationIntent.putExtra(LOCATION_MESSAGE, Parcels.wrap(data));
LocalBroadcastManager.getInstance(this).sendBroadcast(locationIntent);
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
@Override
public void onDestroy() {
stopLocationUpdates();
mGoogleApiClient.disconnect();
super.onDestroy();
}
@Override
public void onConnected(Bundle connectionHint) throws SecurityException {
Log.i(TAG, "Connected to GoogleApiClient");
if (data.mLastLocation == null) {
data.mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
sendUpdates();
}
startLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
sendLocationChanged(location);
Log.d(TAG, "onLocationChanged: " + location.getLatitude() + ", " + location.getLongitude());
sendUpdates();
}
public void sendLocationChanged(Location location) {
//SEND DATA TO THE SERVER
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Nach jeder Location Update Ich bin Berechnung der verstrichenen Zeit (ich muss dies tun, indem ich hinzufüge, weil der Benutzer die Aktivität pausieren kann). Das Problem ist, wenn ich die App in meiner Bürozeit laufen lasse, ist genau (zum Beispiel 35 Minuten), aber wenn ich Fahrrad fahre, ist die Zeit zu kurz (zeigt 15 Minuten, wenn tatsächlich etwa 30 Minuten vergangen sind). Für die Zeit perspective der einzige Unterschied ist, dass es häufiger aufgerufen wird (in Office-GPS wird nur alle 10-20 Sekunden aktualisiert, aber außerhalb kann sogar alle 2 Sekunden). Die Route und die Entfernung sind in Ordnung - Service ist nicht getötet oder so ähnlich. Ich aktualisiere auch die Zeit in der Aktivität, aber ersetze sie bei jedem Update mit der übertragenen (durch den Dienst) Zeit, aber selbst das Entfernen dieses Problems hat das Problem nicht behoben. Warum passiert dies?
Das ist es! Die Verwendung von elapsedRealtime() hat das Problem behoben. Ich habe nie erwartet, dass updateMillis() so funktioniert. – Makalele