手机联网方式的判断
①设置wifi连接
②设置wap连接
③设置net连接
④设置手机网络连接
⑤手机网络设置
一、手机网络连接的几种方式1、wifi连接 2、手机连接 ①net ②wap(有代理ip和端口port,用于增值业务) 3、apn设置(接入点名称:Access Point Name) 4、如果wifi和手机连接一同开启,那么wifi优先级高,手机连接会被屏蔽掉 |
二、源码: ①清单文件声明的内容提供者 \packages\providers\TelephonyProvider\AndroidManifest.xml <provider android:name="TelephonyProvider"
android:authorities="telephony"
android:exported="true"
android:multiprocess="true" /> ②内容提供者的uri \packages\providers\TelephonyProvider\src\com\android\providers\telephony\TelephonyProvider.java
s_urlMatcher.addURI("telephony", "carriers/preferapn", URL_PREFERAPN);
③数据库文件 \data\data\com.android.providers.telephony\databases\telephony.db proxy:如果是wap,代理ip port:如果是wap,代理的port 字段: [_id, name, numeric, mcc, mnc, apn, user, server, password, proxy, port, mmsproxy, mmsport, mmsc, authtype, type, current, protocol, roaming_protocol, carrier_enabled, bearer] |
三、检查网络连接状态的工具类 package cn.zengfansheng.lottery.util;
import org.apache.commons.lang3.StringUtils;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import cn.zengfansheng.lottery.GlobalParams;
/**
* 网络连接的工具
* @author hacket
*/
public class NetUtil {
/**
* 1、检查是否是wifi连接网络
* @return true,wifi连接;false,无网络,或者不是wifi连接
*/
public static boolean isWifiConnected(Context context) {
//权限:ACCESS_NETWORK_STATE
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo == null) {
return false;
}
return networkInfo.isConnected();
}
/**
* 2、检查是否是手机连接
* @return true,手机连接;false,无网络,或者不是手机连接
*/
public static boolean isMobileConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkInfo==null) {
return false;
}
return networkInfo.isConnected();
}
/**
* 3、设置apn连接
* @param context
*/
public static void setApn(Context context) {
//s_urlMatcher.addURI("telephony", "carriers/preferapn", URL_PREFERAPN);
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.parse("content://telephony/carriers/preferapn");
Cursor cursor = resolver.query(uri, null, null, null, null);
//cursor里面的内容:[_id, name, numeric, mcc, mnc, apn, user, server, password, proxy, port, mmsproxy, mmsport, mmsc, authtype, type, current, protocol, roaming_protocol, carrier_enabled, bearer]
if (cursor != null && cursor.getCount() == 1) {// 因为apn只能设置一个
if (cursor.moveToNext()) {
//代理ip
GlobalParams.PROXY_IP = cursor.getString(cursor.getColumnIndex("proxy"));
// 代理端口
GlobalParams.PROXY_PORT = cursor.getString(cursor.getColumnIndex("port"));
}
}
cursor.close();// 关闭游标
}
/**
* 4、检查网络状态
* @param context
* @return
*/
public static boolean checkNetwork(Context context){
// 1、wifi连接
if (isWifiConnected(context)) {
return true;
}
// 2、手机连接,并设置apn
if (isMobileConnected(context)) {
setApn(context);
return true;
}
// 3、当前没有连接网络
return false;
}
/**
* 5、手机是否wap和net
* @return null表示无网络或者wifi连接中,wap,net
*/
public static String isWap(Context context) {
boolean mobileConnected = isMobileConnected(context);
if (!mobileConnected) {
return null;
}
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.parse("content://telephony/carriers/preferapn");
Cursor cursor = resolver.query(uri, null, null, null, null);
if (cursor != null && cursor.getCount() == 1) {// 因为apn只能设置一个
if (cursor.moveToNext()) {
//代理ip
String proxy_ip = cursor.getString(cursor.getColumnIndex("proxy"));
// 代理端口
String proxy_port = cursor.getString(cursor.getColumnIndex("port"));
if (!StringUtils.isBlank(proxy_ip) && !StringUtils.isBlank(proxy_port)) {
return "wap";
}
}
}
cursor.close();
return "net";
}
} |