我的应用程序在后台耗尽了大量电池

时间:2014-10-29 10:00:50

标签: android battery android-background

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

我的位置活动是:

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);
    }


}

我认为这应该可行,但我不确定。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

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

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

mLocationClient.disconnect();

我希望这有帮助!