Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
活动更改时Android GPS未关闭_Android_Gps_Mapactivity - Fatal编程技术网

活动更改时Android GPS未关闭

活动更改时Android GPS未关闭,android,gps,mapactivity,Android,Gps,Mapactivity,我在我的一个Android应用程序上遇到了大量的电池消耗,我相信这是因为GPS被打开了,但在新活动开始时从未关闭。我正在使用从其他人那里继承的代码库。如果你有这个问题的解决方案,请告诉我 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Start the tracker in manual dispatch mode...

我在我的一个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);
        }
    });

你应该使用这个方法

    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等。

我的解决方案对您有用吗?从我的判断,是的,但您能给我一个简单的方法来检查GPS是开还是关的吗。我想在log cat中打印一些东西,以保证它正常工作。谢谢“如果(provider.contains(“gps”)条件是您想要的,我猜。