目前我正在为我的安卓手机构建一个非常小的原生应用程序。当我单击应用程序中的一个按钮时,我想激活便携式 Wi-Fi 热点。但我不知道如何调用 Android API 来激活便携式 Wi-Fi 热点。我知道如何通过 UI 来做到这一点。谁能给我看?
问问题
1195 次
1 回答
0
// code to enable portable wifi hotspot
public void EnableWifiHotspot(){
try{
WifiManager wifi_manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifi_configuration = null;
Method wifiHotspotEnabledMethod=wifi_manager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
wifiHotspotEnabledMethod.invoke(wifi_manager, wifi_configuration, true);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
//code for disabling portable wifi hotspot
public void DisableWifiHotspot(){
try{
WifiManager wifi_manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifi_configuration = null;
Method wifiHotspotEnabledMethod=wifi_manager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
wifiHotspotEnabledMethod.invoke(wifi_manager, wifi_configuration, false);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
于 2014-04-14T10:48:12.667 回答