android LocationServices Api在后台无法获取位置更新

时间:2017-07-01 17:18:25

标签: android

public class LocationService extends BaseService implements
    LocationListener, Orientation.Listener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {                    

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

    @Override        
    public void onCreate() {            
        super.onCreate();                        
        createLocationRequest();                           
        //Location request
        builder mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API)
            .addConnectionCallbacks(this).addOnConnectionFailedListener(this)                   
            .build();            
        mGoogleApiClient.connect();                
    }            

    protected void createLocationRequest() {            
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }                    

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


    @Override        
    public void onConnected(Bundle bundle) {            
        try {                
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);               
        }            
        catch (SecurityException e) { }
    }

    @Override        
    public void onConnectionSuspended(int i) { }         

    @Override        
    public void onLocationChanged(Location location) {    
        // if(location.isFromMockProvider()){
        // Log.d(TAG, "onLocationChanged: From mock location");
        // } else {
        speed = location.getSpeed() * 18 / 5;                
        mCurrentLocation = location;                
        LatLng local = new LatLng(location.getLatitude(), location.getLongitude());
        if (sharedPreferences.getBoolean(Constants.isRiding, false)) {
            locationArray.add(local);    
            //Log.d("location", "onLocationChanged: " + local);
        }                    

        if (lStart == null) {                    
            lStart = mCurrentLocation;                    
            lEnd = mCurrentLocation;                
        } else {           
            lEnd = mCurrentLocation;                
            updateUI();
        }           
    }            

    @Override        
    public void onConnectionFailed(ConnectionResult connectionResult) { }                 

    private void updateUI() {
        // doing something here
    }                          

     public void connect() {
        mGoogleApiClient.connect();
     }                

    public static Location returnLocation() {                
        return mCurrentLocation;        
    }
}

**我正在使用google api客户端和locationservices api来获取后台位置,并且当手机解锁时它工作正常但是当手机被锁定时我没有获得位置更新我做错了什么?

之前曾经有过工作,但我不知道为什么它现在不起作用 **

1 个答案:

答案 0 :(得分:0)

任何未来面临同样问题的人

我从com.google.android.gms:play-services:11.0.2到应用程序gradle中降低了我的位置api com.google.android.gms:播放服务:10.0.2