开关wifi
检查网络是否连通
检查接入点类型,检查网络类型
权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
public static final byte NET_TYPE_UNCONNECT = -1;
public static final byte NET_TYPE_WAP = 0;
public static final byte NET_TYPE_NET = 1;
public static final byte NET_TYPE_WIFI = 2;
public static final byte SIM_TYPE_NOCARD = -1;
public static final byte SIM_TYPE_CM = 0;
public static final byte SIM_TYPE_UNI = 1;
public static final byte SIM_TYPE_3G = 2;
public static final byte SIM_TYPE_CT = 3;
public static final Uri PREFERRED_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");
public static final Uri APN_TABLE_URI = Uri
.parse("content://telephony/carriers");
/**
* 查看wifi 状态}
/**获得当前sim卡 运营商类型
*
* @return
*/
public int getSimType(){
int type =SIM_TYPE_NOCARD;
TelephonyManager phoneManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (phoneManager != null) {
String imsi = phoneManager.getSubscriberId();
if(imsi!=null){
if(imsi.startsWith("46000") || imsi.startsWith("46002")){
//中国移动
type = SIM_TYPE_CM;
}else if(imsi.startsWith("46001")){
//中国联通
type = SIM_TYPE_UNI;
}else if(imsi.startsWith("46003")){
//中国电信
type = SIM_TYPE_CT;
}
}
}
return type;
}
/**
* 获得当前联网类型WIFI OR CMNET OR CMWAP
*
* @return
*/
public int getNetWorkType() {
int type = NET_TYPE_UNCONNECT;
ConnectivityManager m_ConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (m_ConnectivityManager != null) {
NetworkInfo networkInfo = m_ConnectivityManager
.getActiveNetworkInfo();
if (networkInfo != null) {
switch (networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
type= NET_TYPE_WIFI;
break;
case ConnectivityManager.TYPE_MOBILE:
String apn = networkInfo.getExtraInfo();
if (apn != null && apn.indexOf("wap") != -1) {
type= NET_TYPE_WAP;
break;
}else{
type= NET_TYPE_NET;
break;
}
}
}
}
return type;
}
/**
* 根据apn名字获取APN ID
*
* @param name
* @return
*/
public int getApnId(String apnName) {
String tag = "getApnId";
String projection[] = { "_id,apn,type,current,mnc" };// current不为空表示可以使用的APN, current为1的才会显示
int id = -1;
Cursor cr = null;
String apn = null;
String current = null;
try {
cr = resolver.query(APN_TABLE_URI, projection, null, null, null);
if (cr != null) {
cr.moveToLast();
do {
apn = cr.getString(cr.getColumnIndex("apn"));
current = cr.getString(cr.getColumnIndex("current"));
if (apn != null && current != null && "1".equals(current)
&& apn.equals(apnName)
// && mnc.equals("02")
){
id = cr.getInt((cr.getColumnIndex("_id")));
break;
}
} while (cr.moveToPrevious());
cr.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
} finally {
if (cr != null)
cr.close();
}
return id;
}
/**
* 将选中的接入点设置为当前接入点
*/
public boolean SetDefaultAPN(int id) {
boolean res = false;
ContentValues values = new ContentValues();
values.put("apn_id", id);
Cursor c = null;
try {
resolver.update(PREFERRED_APN_URI, values, null, null);
c = resolver.query(PREFERRED_APN_URI,
new String[] { "name", "apn" }, "_id=" + id, null, null);
if (c != null) {
res = true;
c.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
} finally {
if (c != null) {
c.close();
}
}
return res;
}