2013-05-08 30 views
0

目前我正在爲我的android手機構建一個非常小的原生應用程序。我想在應用程序中單擊一個按鈕時激活便攜式Wi-Fi熱點。但我不知道如何調用Android API來激活便攜式Wi-Fi熱點。我知道如何通過用戶界面來做到這一點。任何人都可以告訴我?使用java代碼啓用便攜式wi-fi熱點

回答

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(); 

     } 

} 
+0

謝謝你的男人。 – Bowen 2014-08-12 07:29:26