I am experiencing a mass amount of battery consumption on one of my Android apps, and I believe it is due to the GPS being turned on, but never being turned off when a new activity starts. I am working with a code base that I inherited from someone else. Please let me know if you have a solution to this problem.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start the tracker in manual dispatch mode...
GoogleAnalyticsTracker.getInstance().startNewSession(sessionCode, this);
//Get url to get survey and tags
urlSurveyFilter = Global.getInstance().getUrlStringForRequest(R.string.jsonsurveys, this);
urlJsonTag = Global.getInstance().getUrlStringForRequest(R.string.jsontagrequest,this);
//to use the KinseyAppActivity.this like a global variable
_this = this;
//set the layout from my xml
setContentView(R.layout.main1);
Global.getInstance().locatePosition(MapViewActivity.this);
// button to choice the filter
Button filterButton = (Button) findViewById(R.id.filterButton);
filterButton.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
sendTrackingEvents("Click Filter Button", "Open the filter", "clicked", 1, "/MapActivity");
showDialog(0);
}
});
You should use this method
private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
where you want to stop gps. E.g. this can be onDestroy , onStop etc.