Kontinuierliche Aktualisierung von Standortdaten im hintergrund
ich entwickle eine Anwendung, die sendet Standort-updates kontinuierlich von einem hintergrund-Dienst. ich habe versucht, folgenden code ein.
public class LocationService extends Service implements LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
LocationClient mLocationClient;
@Override
public void onCreate() {
//creating log file in mobile
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service created:", com.example.locationservice.Constants.LOG_FILE);
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(5*1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// mLocationRequest.setFastestInterval(5*1000);
mLocationClient = new LocationClient(getApplicationContext(), this,this);
mLocationClient.connect();
}
@Override
public void onStart(Intent intent, int startId) {
int start = Service.START_STICKY;
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service Started:", com.example.locationservice.Constants.LOG_FILE);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//TODO Auto-generated method stub
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Connection to client failed", com.example.locationservice.Constants.LOG_FILE);
this.stopSelf();
}
@Override
public void onConnected(Bundle arg0) {
//TODO Auto-generated method stub
Log.i("info", "Location Client is Connected");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Client Connectd:", com.example.locationservice.Constants.LOG_FILE);
//checking for locaton enabled or not
if(Util.isLocationEnabled(getApplicationContext())){
//checking for internet available or not
if(Util.isInternetOn(getApplicationContext())){
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}else{
Log.i("info", "Internet not available");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Internet not available", com.example.locationservice.Constants.LOG_FILE);
this.stopSelf();
}
}else{
Log.i("info", "Location Acess disabled");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Acess disabled", com.example.locationservice.Constants.LOG_FILE);
this.stopSelf();
}
Log.i("info", "Service Connect status :: " + isServicesConnected());
}
@Override
public void onDisconnected() {
//TODO Auto-generated method stub
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Client DisConnectd:", com.example.locationservice.Constants.LOG_FILE);
Log.i("info", "Location Client is DisConnected");
}
@Override
public void onLocationChanged(Location location) {
//TODO Auto-generated method stub
double latitude = location.getLatitude();
double longitude = location.getLongitude();
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Changed:", com.example.locationservice.Constants.LOG_FILE);
Log.i("info", "Latitude :: " + latitude);
Log.i("info", "Longitude :: " + longitude);
if(Util.isInternetOn(getApplicationContext())){
//sending location details
sendLocation(location);
}else{
this.stopSelf();
Log.i("info", "Internet not available");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Internet not available", com.example.locationservice.Constants.LOG_FILE);
}
}
@Override
public IBinder onBind(Intent intent) {
//TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
//TODO Auto-generated method stub
Log.i("info", "Service is destroyed");
mLocationClient.removeLocationUpdates(this);
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service Destroyed:", com.example.locationservice.Constants.LOG_FILE);
super.onDestroy();
}
private boolean isServicesConnected() {
//Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationService.this);
//If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
return true;
} else {
return false;
}
}
}
aber onLocationChanged
anruft, nur wenn ich öffnen Sie die eingebaute Karte Anwendung. andere weisen, es ist nicht die Aktualisierung der Standortinformationen.
ich begann, diesen service von einer Aktivität mit alarm-service. alarmManager
Trigger für jede minute. kann einer mir sagen, warum onLocationChanged ruft nicht kontinuierlich.
Vielen Dank im Voraus.
- Sind Sie sicher, dass das Gerät Zeit hat, um sich einen GPS first fix (als Sie erwarten hohe Genauigkeit). Wenn es hält, Sie zu belästigen, Sie können versuchen, mein code aus dieser Antwort stackoverflow.com/questions/19365035/.... Habe mit erfolgreich bei mehreren Gelegenheiten.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Versuchen Sie dies:
ersetzen Sie Ihre onStart mit:
und:
Aufrufen Ihrer service:
Können Sie auch überprüfen, mein code hier
Ist es in Ordnung, die in den hintergrund? Dienst wird nicht beendet nach 45 Minuten, während im hintergrund ausgeführt werden ? – Noman Nov 3 '15 6:18
Es ist ein guter Kommentar. Ich hatte einige proglem mit dem location update lange continious in meinem Projekt. Und es hörte tatsächlich nach 45 Minuten.
Mein service ist Vordergrund. Ich mit
Und ich alle 45 Minuten aufwachen location update
Und timerTask
Es ist sehr einfach und es ist sehr gute Arbeit.
Genießen Sie