0

私はアンドロイドアプリケーションを作成しています。私は、デバイスが静止していて、300または400mの距離を移動してから再度位置を取得すると、位置の更新を停止したいと考えています。私は、場所の更新を継続的にチェックし、バッテリを消費するため、以前の場所と現在の場所の距離を測定したくありません。 現在地サービスクラスコード:デバイスが移動していない場合には位置更新を停止し、300mの距離を移動してその位置を取得すると

public class LocationService extends Service implements 
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

private Looper mServiceLooper; 
private ServiceHandler mServiceHandler; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 

private final class ServiceHandler extends Handler { 
    public ServiceHandler(Looper looper) { 
     super(looper); 
    } 
} 

@Override 
public void onCreate() { 
    HandlerThread thread = new HandlerThread("ServiceStartArguments", 
      Process.THREAD_PRIORITY_BACKGROUND); 
    thread.start(); 

    // Get the HandlerThread's Looper and use it for our Handler 
    mServiceLooper = thread.getLooper(); 
    mServiceHandler = new ServiceHandler(mServiceLooper); 
} 

public void buildGoogleApiClient() { 
    createLocationRequest(); 
    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 
} 

protected void createLocationRequest() { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(10000); 
    mLocationRequest.setFastestInterval(5000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLocationRequest.setSmallestDisplacement(500); 
} 

public void startLocationUpdates() { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(
      mGoogleApiClient, mLocationRequest, this); 
} 

protected void stopLocationUpdates() { 
    LocationServices.FusedLocationApi.removeLocationUpdates(
      mGoogleApiClient, this); 
    mGoogleApiClient.disconnect(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 
    buildGoogleApiClient(); 
    return START_STICKY; 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onDestroy() { 
    // The service is no longer used and is being destroyed 
    stopLocationUpdates(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    System.out.println("Latitude: " + location.getLatitude()); 
    System.out.println("Longitude: " + location.getLongitude()); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    startLocationUpdates(); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

IはlocationRequest.setSmallestDisplacement(500)を使用していますが、以前と現在位置との距離が例えば未満500メートルである場合、それは、連続的にのみonLocationChangedメソッドが呼び出されていない位置の更新を受信します

この目的のためにジオフェンシングを使用すべきですか? 最低限のバッテリ消費でFusedLocationApiを使用してこれを達成する最良の方法は何ですか?

答えて

1

ここで注意すべきいくつかのことがあります。

  1. をお使いのデバイスが動いていない場合は、位置サービスは、バッテリーのために最適化され、必ずしも高価な新しい位置検索を行いません。したがって、あなた自身でこれを最適化しようとする必要はありません。
  2. LocationRequest#setSmallestDisplacementに小さい値を渡すことを検討してください。
  3. バッテリーを最適化するには、の値に基づいてデバイスに配信される、バッチ処理されたロケーションの更新を使用することを検討してください(ロケーションは、LocationRequest#setIntervalに記載されている間隔で計算されます)。これはバッテリーに非常に役立ちます。

あなたの質問の一部ではありませんが、場所の更新を要求したり削除したりするためのコードを少し違った方法で構成します。 GoogleApiClientonStart()に接続し、onStop()で切断します。 onConnected()onResume()requestLocationUpdates()と電話し、をonPause()またはonStop()で呼び出しますが、遅くともonDestroy()ではありません。

関連する問題