活动更改时,Android GPS不会关闭

时间:2013-11-04 14:24:03

标签: android gps mapactivity

我在我的某个Android应用上遇到了大量的电池消耗,我相信这是由于GPS已开启,但在新活动开始时从未关闭过。我正在使用我从其他人继承的代码库。如果你能解决这个问题,请告诉我。

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

1 个答案:

答案 0 :(得分:0)

您应该使用此方法

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

你要停止gps的地方。例如。这可以是onDestroy,onStop等。