2016-01-21 49 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。