我正在開發使用地理定位的應用程序。問題是,當用戶沒有關閉它並且應用程序處於後臺時,它會耗盡大量電量,我不知道問題出在哪裏。我的應用程序在背景中耗盡大量電池
我的位置活動是這樣的:
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);
}
}
我想這應該能正常運行,但我不知道。非常感謝您的幫助。
當應用程序在後臺時,您的應用程序是否停止獲取位置更新? – TomerZ 2014-10-29 10:15:16
是的,logcat顯示已成功調用onStop方法,並且操作欄中的位置圖標消失。 – Yeray 2014-10-29 10:31:46