2011-04-22

How to toggle power controls (Wi-Fi, GPS, bluetooth, brightness, sync)

This topic has been asked on Android forums regularly and sometimes you are told not to turn on/off GPS/Bluetooth without consents from users. That's true.

Despite this consent issue, what is the simplest way to toggle power controls programmatically? For Android 2.1+, my opinion is to take advantage of the code Settings already provides.

Intent intent = new Intent();
intent.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("custom:" + getButtonId()));
context.sendBroadcast(intent);

The getButtonId() returns the button id on the PowerControl widget. You can try to put this widget on your device. It is not impossible that these button ids are changed in the future Android release or by vendors. But, I have to say it's not very likely to happen.

  • Wi-Fi:  0
  • Brightness: 1
  • Sync: 2
  • GPS: 3
  • Bluetooth: 4

Another advantage of using this is the reduction of hardware permissions you have to declare.