0

i need to toggle wifi on and off when user clicks a widget icon...

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
if(wifiManager.isWifiEnabled())
    wifiManager.setWifiEnabled(false);
else
    wifiManager.setWifiEnabled(true);

How do i implement it using pendingIntents?

Amel Jose
  • 532
  • 5
  • 9

3 Answers3

1

Step #1: Create a BroadcastReceiver that contains your setWifiEnabled() logic.

Step #2: Declare that receiver with a <receiver> element in the manifest.

Step #3: Use a getBroadcast() PendingIntent with the button in your app widget, pointing to this BroadcastReceiver.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Download below example it will help you

https://github.com/siddhpuraamitr/WIfi-Toggle-Widget

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
0

From Android-10 (API level 29)

According to documentation, Apps will not be able to turn Wi-Fi OFF/ON programmatically anymore from Android-10 API level 29[Until google provides an alternative solution].

See this Answer for details.

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29