我正在开发一个使用地理位置的应用程序。问题是当用户不关闭它并且应用程序处于后台时,它会消耗大量电池,我不知道问题出在哪里。
我的位置活动是这样的:
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);
}
}
我认为这应该可以正常工作,但我不确定。非常感谢您的帮助。