简体   繁体   English

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

[英]Android GPS Not Turning Off When Activity Is Changed

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. 我的一个Android应用程序正在消耗大量电池,我相信这是由于GPS开启了,但是在新活动开始时却从未关闭过。 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. 您要停止gps的位置。 Eg this can be onDestroy , onStop etc. 例如,可以是onDestroy,onStop等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM