2016-03-31 11 views
-1

Merhaba Bir Android App oluşturuyorum. bir Navigasyon modülü dahil ediyorum. Burada kullanıcının konumunu 20 saniyelik bir aralıkta otomatik olarak kontrol etmek istiyorum, bu konum değişikliği olayını tetiklemek için herhangi bir tuşa basmak istemiyorum. Bu yöntem, gerçek zamanlı Google Map navigasyon sistemi gibi otomatik olarak çalışmalıdır. Kullanıcının konumunu FusedLocationAPI ve LocationListener kullanarak getiriyorum. Bu yöntemi oluşturdum. İşteKullanıcının Konumunu periyodik olarak 20 San. otomatik olarak

private void onLocationUpdate() { 
    lr.setInterval(20000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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(googleApiClient, lr, (com.google.android.gms.location.LocationListener) this); 

} 

lrLocationRequest bir nesnedir. Lütfen bana bu yöntemi nerede aramalıyım söyle. Ve onLocationChanged(Location location)'un altında Kullanıcının geçerli konumunda işaretleyici ekliyorum. Bir hizmeti oluşturmanız gerekecektir

cevap

0

:

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

ve gerektiğinde yöntemi uygulamak. Aldığınız zaman X kere geçip geçmediğinizi ve eğer konumla bir şeyler yapıp yapmamanız gerektiğini kontrol etmek için size gelecektir. Ayrıca, gradeline üzerinde

compile 'com.google.android.gms:play-services-location:8.+' 
    compile 'com.google.android.gms:play-services-maps:8.+' 

'u eklemeyi unutmayın. Ve tezahürünüzde, elbette izin ekleyin;

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

ve servis:

<service 
     android:name="{package}.LocationService" 
     android:exported="false" /> 
İlgili konular