Android 自动开启WiFi热点

android开发中自动开启wifi热点 更多

在android开发中遇到自动开启wifi热点这一项,浏览了一些博客大多说的很杂乱,我在这里根据我的开发经验整理一下,只说开启wifi热点而不说连接问题,避免答非所问。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private WifiManager wifiManager;
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
setWifiApEnabled(true/false);
// wifi热点开关 开启传true,关闭传false
public boolean setWifiApEnabled(boolean enabled) {
if (enabled) { // disable WiFi in any case
//wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi
wifiManager.setWifiEnabled(false);
} else {
// wifiManager.setWifiEnabled(true);
}
try {
//热点的配置类
WifiConfiguration apConfig = new WifiConfiguration();
//配置热点的名称(可以在名字后面加点随机数什么的)
apConfig.SSID = "YOURSSID";
//配置热点的密码
apConfig.preSharedKey="YOURPASS";
//安全:WPA2_PSK
//apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//通过反射调用设置热点
Method method = wifiManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
//返回热点打开状态
return (Boolean) method.invoke(wifiManager, apConfig, enabled);
} catch (Exception e) {
Log.e(TAG, "Cannot set WiFi AP state", e);
return false;
}
}

Android安卓获取网络状态 更多

首先在AndroidManifest.xml添加权限

1
<uses-permission Android:name="android.permission.ACCESS_NETWORK_STATE" />

1.判断是否有网络连接

(1)第一种方法

1
2
3
4
5
6
7
8
9
10
11
public boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}

(2)第二种方法

1
2
3
4
5
6
public static boolean isNetworkConnected(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}

2.判断WIFI网络是否可用

1
2
3
4
5
6
7
8
9
10
11
12
public boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}

3.判断MOBILE网络是否可用

1
2
3
4
5
6
7
8
9
10
11
12
public boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}

4.判断网络类型

网络连接状态:

1
2
3
4
5
ConnectivityManager.TYPE_MOBILE 手机网络
ConnectivityManager.TYPE_WIFI 无线网咯
ConnectivityManager.TYPE_WIMAX 暂时不知
ConnectivityManager.TYPE_ETHERNET 以太网
ConnectivityManager.TYPE_BLUETOOTH 蓝牙
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//返回值 -1:没有网络 1:WIFI网络 2:wap网络 3:net网络 4 以太网
public static int GetNetype(Context context){
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
/*if(networkInfo==null){
return netType;
}*/
int nType = networkInfo.getType();
if(nType==ConnectivityManager.TYPE_MOBILE){
if(networkInfo.getExtraInfo().toLowerCase().equals("cmnet")){
netType = 3;
} else {
netType = 2;
}
} else if(nType==ConnectivityManager.TYPE_WIFI){
netType = 1;
} else if (nType == ConnectivityManager.TYPE_ETHERNET) {
netType = 4;
}
return netType;
}

Android WiFi开发 (二)Wifi热点 更多