当活动改变时,Android GPS不关闭

当活动改变时,Android GPS不关闭

问题描述:

我的Android应用程序中有一个电池消耗量很大,我相信这是由于打开了GPS,但从未在新活动时关闭开始。我正在使用从其他人继承的代码库。请让我知道你是否有解决这个问题的办法。当活动改变时,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); 
     } 
    }); 
+0

做我的解决方案为你工作? – alicanbatur

+0

从我能说的是,但是你能否给我一个简单的方法来检查GPS是打开还是关闭。我想在log cat中打印一些东西以保证它正在工作。谢谢! –

+0

“if(provider.contains(”gps“)”condition是你想要的,我猜。 – alicanbatur

你应该使用这个方法

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等