GPS ON or OFF without Viewing Settings Page.

I've searched for GPS ON and OFF without passing intent for getting Settings page . Now,I got a solution for that .. To TurnOn, I used the snippets below,
String set = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!set.contains("gps")){ 
            final Intent gpsIntent = new Intent();
             gpsIntent .setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
             gpsIntent.addCategory(Intent.CATEGORY_ALTERNATIVE);
             gpsIntent.setData(Uri.parse("3")); 
            sendBroadcast(gpsIntent);
}
and to TurnOFF,
String set1 = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(set1.contains("gps"))
         {
         final Intent  gpsIntent1= new Intent();
          gpsIntent1.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
          gpsIntent1.addCategory(Intent.CATEGORY_ALTERNATIVE);
          gpsIntent1.setData(Uri.parse("3")); 
         sendBroadcast(gpsIntent1);
}
You can test by seeing the settings page after every On and Off To get Full Source Code Click Here

Comments

  1. Not working for me can you please provide the correct solution.

    ReplyDelete

Post a Comment

Popular Posts