How to turn on the GPS on Android

后端 未结 7 594
[愿得一人]
[愿得一人] 2020-12-08 08:52

I am developing an android app which needs to activate the GPS.

I read a lot of topics in a lot of forums and the answer I\'ve found is:

it\'

相关标签:
7条回答
  • 2020-12-08 09:14

    you might want to check out this thread

    How can I enable or disable the GPS programmatically on Android?

    here are the codes copied from that thread

    private void turnGPSOn(){
        String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    
        if(!provider.contains("gps")){ //if gps is disabled
            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);
        }
    }
    
    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);
    
    
       }
    }
    

    but the solution is not recommended as it will not be available to android version > 2.3 supposingly.. do check the comments

    0 讨论(0)
提交回复
热议问题