Android以编程方式打开/closuresWiFi HotSpot
有没有一个API以编程方式打开/closuresAndroid上的WiFi热点?
我应该打什么方法打开/closures?
更新:有这个选项来启用热点,并打开/closuresWiFi,但这不是一个很好的解决scheme。
使用下面的类来更改/检查Wifi热点设置:
import android.content.*; import android.net.wifi.*; import java.lang.reflect.*; public class ApManager { //check whether wifi hotspot on or off public static boolean isApOn(Context context) { WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE); try { Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled"); method.setAccessible(true); return (Boolean) method.invoke(wifimanager); } catch (Throwable ignored) {} return false; } // toggle wifi hotspot on or off public static boolean configApState(Context context) { WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE); WifiConfiguration wificonfiguration = null; try { // if WiFi is on, turn it off if(isApOn(context)) { wifimanager.setWifiEnabled(false); } Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.invoke(wifimanager, wificonfiguration, !isApOn(context)); return true; } catch (Exception e) { e.printStackTrace(); } return false; } } // end of class
您需要将以下权限添加到您的AndroidMainfest中:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
在任何地方使用这个独立的ApManager类,如下所示:
ApManager.isApOn(YourActivity.this); // check Ap state :boolean ApManager.configApState(YourActivity.this); // change Ap state :boolean
希望这会帮助别人
Android SDK中没有与WiFi热点function有关的方法 – 对不起!
您可以使用以下代码以编程方式启用,禁用和查询wifi直接状态。
package com.kusmezer.androidhelper.networking; import java.lang.reflect.Method; import com.google.common.base.Preconditions; import android.content.Context; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.util.Log; public final class WifiApManager { private static final int WIFI_AP_STATE_FAILED = 4; private final WifiManager mWifiManager; private final String TAG = "Wifi Access Manager"; private Method wifiControlMethod; private Method wifiApConfigurationMethod; private Method wifiApState; public WifiApManager(Context context) throws SecurityException, NoSuchMethodException { context = Preconditions.checkNotNull(context); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiControlMethod = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,boolean.class); wifiApConfigurationMethod = mWifiManager.getClass().getMethod("getWifiApConfiguration",null); wifiApState = mWifiManager.getClass().getMethod("getWifiApState"); } public boolean setWifiApState(WifiConfiguration config, boolean enabled) { config = Preconditions.checkNotNull(config); try { if (enabled) { mWifiManager.setWifiEnabled(!enabled); } return (Boolean) wifiControlMethod.invoke(mWifiManager, config, enabled); } catch (Exception e) { Log.e(TAG, "", e); return false; } } public WifiConfiguration getWifiApConfiguration() { try{ return (WifiConfiguration)wifiApConfigurationMethod.invoke(mWifiManager, null); } catch(Exception e) { return null; } } public int getWifiApState() { try { return (Integer)wifiApState.invoke(mWifiManager); } catch (Exception e) { Log.e(TAG, "", e); return WIFI_AP_STATE_FAILED; } } }
你最好的select是看WifiManager类。 具体来说就是setWifiEnabled(bool)
函数。
请参阅以下文档: http : //developer.android.com/reference/android/net/wifi/WifiManager.html#setWifiEnabled(boolean)
如何使用它的教程(包括你需要什么权限)可以在这里find: http : //www.tutorialforandroid.com/2009/10/turn-off-turn-on-wifi-in-android-using.html
这适用于我:
WifiConfiguration apConfig = null; Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE); method.invoke(wifimanager, apConfig, true);
我已经发布了非官方的API,它包含的不止是热点开启on/off
。 链接
对于API的DOC 链接 。
WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(status);
状态可能是true
或是false
添加权限清单: <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />