2016-07-22 77 views
0

以下是我的位置服务类,如果设备更改位置,它会继续更新位置。融合位置提供商GoogleApiClient有时不会更新后台服务中的位置

public class LocationService extends Service 
     implements LocationListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 
    private static final long INTERVAL = 1000 * 60; 
    private static final long FASTEST_INTERVAL = 1000 * 5; 
    Location mLastLocation; 
    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 
    String lat, lon; 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.d("", "********************** onStartCommand()"); 
     int permissionCheck = ContextCompat.checkSelfPermission(LocationService.this, Manifest.permission.ACCESS_FINE_LOCATION); 

     if (permissionCheck == PackageManager.PERMISSION_GRANTED) { 
      //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION.. 
      buildGoogleApiClient(); 
     } 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     Log.d("", "********************** onBind()"); 
     return null; 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.d("", "********************** onConnected()"); 
//  Util.appendLog("Location Service onConnected()"); 
     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setInterval(INTERVAL); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      lat = String.valueOf(mLastLocation.getLatitude()); 
      lon = String.valueOf(mLastLocation.getLongitude()); 

      Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + lat); 
      Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + lon); 

      Util.appendLog("Location Service onConnected(): " + " Latitude: " + lat + " Longitude: " + lon); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Util.appendLog("Location Service onConnectionSuspended()"); 
    } 

    synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("", "********************** onLocationChanged()"); 
//  Util.appendLog("Location updated Latitude: " + location.getLatitude() + " longitude: " + location.getLongitude()); 
//  Toast.makeText(LocationService.this, "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(), Toast.LENGTH_SHORT).show(); 
     if (location != null) { 
      Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + location.getLatitude()); 
      Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + location.getLongitude()); 
     } 
//  stopSelf(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Util.appendLog("Location Service onConnectionFailed()"); 
     buildGoogleApiClient(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     Util.appendLog("Location servcie destroyed()"); 
    } 
} 

清单中我已经声明如下这种服务:

<service 
      android:name=".util.LocationService" 
      android:enabled="true" /> 

我开始每次都是这样,当用户启动应用程序在他/她的设备

if (!isMyServiceRunning(LocationService.class, context)) { 
        context.startService(new Intent(context, LocationService.class)); 
} 

注:我我没有停止从任何地方的服务。

问题:在所有的大品牌设备,如谷歌Nexus 5,金立,一+,三星....但在某些设备位置不更新时网络或无线网络不可用类似的荣誉,XOLO

  1. 位置更新...
  2. 每次当室外或驾车onLocationChanged()方法应该被调用,并在WriteSharePrefrence()新的更新的经度和纬度应该改变,但它不是在一些设备频繁更新。
  3. 我的服务有什么问题吗?我的应用中没有电池耗尽事项。
  4. 有什么是停止我的服务?
  5. 如何在后台继续运行位置更新服务并经常从用户设备获取准确的位置更新?
+0

是onDestroy被调用?如果设备内存不足,操作系统可能会导致您的服务中断。尝试添加标志 - Service.START_STICKY – X3Btel

+0

@ X3Btel onDestrye不能呼叫。 Service.START_STICKY发生了什么? – himCream

+0

如果系统杀死它,启动粘性重启服务。但是,如果onDestroy没有被调用,它应该没有帮助。其他设备是否也打开gps,具有高精度? – X3Btel

回答

1

Google的Play Services API已经可以很好地处理这种情况。

在此请看:FusedLocationProviderApi requestLocationUpdates

您应该使用以下功能:

public abstract PendingResult<Status> requestLocationUpdates (GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent) 

从链接的关键亮点:

此方法适用于后台用例,更具体的用于接收位置更新,即使应用程序已被系统杀死。对于前景的使用情况下,建议在LocationListener版本的方法的...

只需使用的requestLocationUpdates的的PendingIntent版本,你应该开始接收可靠的后台位置更新。

相关问题