0

我正在开发一个使用地理位置的应用程序。问题是当用户不关闭它并且应用程序处于后台时,它会消耗大量电池,我不知道问题出在哪里。

我的位置活动是这样的:

public abstract class LocationActivity extends FragmentActivity implements LocationListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
    /**
     * Variables.
     */
    private     AppGlobal           global;
    private     String              TAG     =   "LOCATIONACTIVITY";
    private     LocationRequest     mLocationRequest;
    private     LocationClient      mLocationClient;
    private     LocationListener    listener;



    /**
     * 
     */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(getLayoutResourceId());

        global = ((AppGlobal)getApplicationContext());

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setSmallestDisplacement(1);

        mLocationClient = new LocationClient(this, this, this);
        if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS)
            mLocationClient.connect();
    }



    /**
     * 
     */
    protected abstract int getLayoutResourceId();



    /**
     * 
     */
    @Override
    public void onStop() {
        Log.v(TAG, "onStop");
        if (mLocationClient.isConnected()) {
            stopPeriodicUpdates();
        }
        mLocationClient.disconnect();
        super.onStop();
    }



    /**
     * 
     */
    @Override
    public void onPause() {
        super.onPause();
        Log.v(TAG, "onPause");
        if (mLocationClient.isConnected()) {
            //mLocationClient.removeLocationUpdates(listener);
            stopPeriodicUpdates();
        }
    }



    /**
     * 
     */
    @Override
    public void onResume() {
        Log.v(TAG, "onResume");
        super.onResume();

        if(mLocationClient.isConnected()) {
            if(listener == null)
                listener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    global.latitude = location.getLatitude();
                    global.longitude = location.getLongitude();
                }
            };
            mLocationClient.requestLocationUpdates(mLocationRequest, listener);
        } else
            mLocationClient.connect();
    }



    /**
     * 
     */
    private boolean servicesConnected() {
        Log.v(TAG, "servicesConnected");
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (ConnectionResult.SUCCESS == resultCode) {
            Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available));
            return true;
        } else {
            return false;
        }
    }



    /**
     * 
     */
    public void getLocation(View v) {
        Log.v(TAG, "getLocation");
        if (servicesConnected()) {
            Location currentLocation = mLocationClient.getLastLocation();
            global.latitude = currentLocation.getLatitude();
            global.longitude = currentLocation.getLongitude();
        }
    }



    /**
     * 
     */
    @Override
    public void onConnected(Bundle bundle) {
        Log.v(TAG, "onConnected");
        startPeriodicUpdates();
    }



    /**
     * 
     */
    @Override
    public void onDisconnected() {
        Log.v(TAG, "onDisconnected");
    }



    /**
     * 
     */
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.v(TAG, "onConnectionFailed");
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            //void
        }
    }



    /**
     * 
     */
    @Override
    public void onLocationChanged(Location location) {
        Log.v(TAG, "onLocationChanged");
        global.latitude = location.getLatitude();
        global.longitude = location.getLongitude();
        Log.v(TAG, "lat:" + global.latitude);
        Log.v(TAG, "lon:" + global.longitude);
    }



    /**
     * 
     */
    public void startPeriodicUpdates() {
        Log.v(TAG, "startPeriodicUpdates");
        mLocationClient.requestLocationUpdates(mLocationRequest, this);
    }



    /**
     * 
     */
    private void stopPeriodicUpdates() {
        Log.v(TAG, "stopPeriodicUpdates");
        mLocationClient.removeLocationUpdates(this);
    }


}

我认为这应该可以正常工作,但我不确定。非常感谢您的帮助。

4

1 回答 1

1

您可以尝试将 LocationRequest 的准确性调整为 PRIORITY_BALANCED_POWER_ACCURACY 并将更长的 setInterval() 和更快的 setFasterInterval() 设置为 60000 毫秒(一分钟)。现在,您要求您的提供商每秒刷新一次位置。如果信息不是那么敏感并且您不需要如此快速的更新,那么这将是降低功耗的好方法。

此外,应用程序可以在 onPause() 方法中进入后台,尝试断开客户端

mLocationClient.disconnect();

我希望这有帮助!

于 2014-10-29T10:18:23.713 回答