2016-07-22 6 views
0

以下は自分のロケーションサービスクラスです。デバイスが変更された場合は、ロケーションを更新し続けます。融合ロケーションプロバイダGoogleApiClientはバックグラウンドサービスのロケーションを更新しないことがあります

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

ユーザーは、彼/彼女のデバイスに

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

注アプリを起動したときに、私は、このたび始めています:マニフェストに

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()"); 
    } 
} 

私は以下のようにこのサービスを宣言している私は、どこからでもサービスを停止していません。

問題:

  1. Googleのネクサス5、gionee、ワン+、サムスンのようなすべてのビッグブランドのデバイスで....しかし、名誉などのインターネットや無線LANが利用できないとき、更新されないいくつかのデバイスの場所に位置更新、xolo ....
  2. 屋外または運転中のデバイスonLocationChanged()メソッドを呼び出す必要があり、WriteSharePrefrence()の新しい更新された緯度と経度を変更する必要がありますが、一部のデバイスでは頻繁に更新されません。
  3. サービスに間違いがありますか?私のアプリにはバッテリーの消耗品はありません。
  4. サービスを停止しているものはありますか?
  5. バックグラウンドでロケーションアップデートサービスを継続し、ユーザデバイスからの正確なロケーションアップデートを頻繁に取得するにはどうすればよいですか?
+0

は、onDestroyが呼び出されていますか?デバイスのRAMが不足している場合、OSによってサービスが停止する可能性があります。 flag- Service.START_STICKY – X3Btel

+0

@ X3Btel onDestryeを追加してみてください。サービスで何が起こったのですか?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のバージョンを使用して、あなたは確実にバックグラウンドの位置情報の更新の受信を開始する必要があります。

関連する問題