使用Widget打开Wifi

时间:2012-11-12 17:25:21

标签: android

当用户点击小部件图标时,我需要打开和关闭wifi ...

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

如何使用pendingIntents实现它?

3 个答案:

答案 0 :(得分:0)

步骤1:创建包含BroadcastReceiver逻辑的setWifiEnabled()

步骤2:声明清单中带有<receiver>元素的接收者。

步骤3:在您的应用小部件中使用getBroadcast() PendingIntent,并指向此BroadcastReceiver

答案 1 :(得分:0)

下面的例子下载它会帮助你

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

答案 2 :(得分:0)

从Android-10(API级别29)

根据documentation,应用将无法通过编程方式从Wi-Fi OFF/ON转到Android-10 API level 29 [直到Google提供替代解决方案]

有关详细信息,请参见this Answer