2016-07-16 9 views
2

現在の場所を取得するサービスのアンドロイドの例を使用していますが、うまくいきますが、どのような変更が必要なのか知りたいのですが、特定の時間間隔(例えば、2分ごとなど)で位置を特定することができる。特定の間隔で実行するようにAndroidサービスを変更する

public class YourService extends Service implements LocationListener { 
    LocationManager mLocationManager; 

public IBinder onBind(Intent intent) { 
    // This won't be a bound service, so simply return null 
    Log.v("WEAVER_", "Service_OnBind..."); 
    return null; 
} 

@Override 
public void onCreate() { 
    // This will be called when your Service is created for the first time 
    // Just do any operations you need in this method. 
    Log.v("WEAVER_", "Service_OnCreate..."); 

    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 
      1, this); 


} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return super.onStartCommand(intent, flags, startId); 
} 
@Override 
public void onDestroy(){ 
    super.onDestroy(); 
    Log.v("WEAVER_","Service_OnDestroy"); 
    mLocationManager.removeUpdates(this); 
    mLocationManager = null; 
} 
@Override 
public void onLocationChanged(Location location) { 
    Log.v("WEAVER_","Service_Location Change"); 

    String msg = "New Latitude: " + location.getLatitude() 
      + "New Longitude: " + location.getLongitude(); 








     Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 



@Override 
public void onProviderDisabled(String provider) { 

} 

}

答えて

0

私はこのためにあなたの正確な意図を知りませんが、あなたはIntentService代わりのサービスを使用してを検討することは価値があるかもしれません。 IntentServiceは、メインスレッドではなく別のスレッドで実行され、開始することもできます。インテントサービスは、実行する予定の処理を実行します。つまり、ロケーションをフェッチして終了します。その後

持続時間のためにそこにこれを行うにはいくつかの方法があるが、私は見て持っているあなたをお勧めします:あなたがIntentServiceのために行く場合は、単純に(いくつかのコンテキスト)を呼び出す https://developer.android.com/training/scheduling/alarms.html

.startServiceときAlarmManagerがトリガします。 代わりに、サービスを常にアクティブにすることができます(onStartCommandでSTART_STICKYを返した場合)。

0

ここで私は間隔を置いて位置を確認することができました。これはGoogleAPIClientを使用しているため、これを機能させるにはプロジェクトに追加する必要があります。このソリューションでは、LocationRequestおよびFusedLocationProviderApiも使用する必要があります。希望このことができます

@Override // This is called when googleApiClient has connected 
public void onConnected(@Nullable Bundle bundle) { 
    fusedLocation.requestLocationUpdates(<googleApiClient here>, locationRequest, this); 
} 

:、その後

public class YourService extends Service implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener { 
    private static GoogleApiClient googleApiClient; 
    private static FusedLocationProviderApi fusedLocation = LocationServices.FusedLocationApi; 
    private static LocationRequest locationRequest; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

     locationRequest = new LocationRequest(); 
     locationRequest.setInterval(5000); // look at provider every 5 seconds 
     locationRequest.setFastestInterval(1000); // or one second if its convenient 
     locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

     googleApiClient.connect(); 
     ... // other code here 
    } 

ようfusedLocationになLocationRequestを追加します。

はこのようGoogleAPIClientを宣言します。もしあなたが望むなら、timertaskを使ってこのようなことをすることもできます。

関連する問題