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连接************************** /** 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、手机连接(wap、net)************************** / 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();
}
/**************************
* 2、设置apn************************** 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、检查是否有网络连接(wifi,手机)************************** /** 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,或者没有网络连接或者wifi连接 ************/
/**
* 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";
}
}