2016-01-21 78 views
3

我正在開發一個位置應用程序,當用戶按下Button時,應該開始跟蹤一些基於經度和緯度的統計數據。所有這些東西工作得很好,但當涉及到鎖定屏幕或將應用程序置於後臺時,該服務不再起作用!Android位置服務在後臺不工作

我已閱讀了很多關於後臺服務和廣播接收器的內容,但我不知道如何在服務中實現Google API位置偵聽器,以及在MainActivity中實現此類的位置。任何人都可以告訴我一個簡短的代碼示例如何實現這樣的服務或鏈接,這是解釋?

回答

3

使用保險絲位置服務和保存更新的位置每次

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

    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 
    public static Location mCurrentLocation; 

    ............................. 

    @Override 
    public void onCreate() { 

     //show error dialog if GoolglePlayServices not available 
     if (isGooglePlayServicesAvailable()) { 
      mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(BACKGROUND_INTERVAL); 
     mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     //mLocationRequest.setSmallestDisplacement(10.0f); /* min dist for location change, here it is 10 meter */ 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(LocationServices.API) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 

      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return super.onStartCommand(intent, flags, startId); 
    } 

    //Check Google play is available or not 
    private boolean isGooglePlayServicesAvailable() { 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
     return ConnectionResult.SUCCESS == status; 
    } 


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

    protected void startLocationUpdates() { 
     try { 
      PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, mLocationRequest, this); 
     } catch (IllegalStateException e) {} 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     //Save your location 
    } 
    ............................ 
} 

就可以得到當前的位置onLocationChanged()

有關詳情,請http://javapapers.com/android/android-location-fused-provider/或按照官方指導https://developer.android.com/training/location/index.html

+0

感謝,這正是我正在尋找!如何在MainActivity中調用此服務來顯示數據? – madrid11

+0

如果它解決了你的問題,那麼你可以接受它。 – Kunu

+0

如何在MainActivity中調用reciver?如何使用MainActivity中服務提供的數據? – madrid11

0

這裏是應用程序,它確實像你想:

https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/CosyDVR.java

主要活動在BackgroundVideoRecorder服務啓動。

Intent intent = new Intent(/*CosyDVR.this*/getApplicationContext(), BackgroundVideoRecorder.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startService(intent); 
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 

看看mConnection onServiceConnected,onServiceDisconnected。

這裏是BackgroundVideoRecorder類:

https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/BackgroundVideoRecorder.java

它實現LocationListener的。所以有onLocationChanged,onProviderDisabled,onProviderEnabled,onStatusChanged。