判断接入的网络类型(wap,net,wifi)

package com.autonavi.cmccmap2_2.util;


import com.autonavi.cmccmap2_2.R;
import com.autonavi.cmccmap2_2.act.map.MapHelper;
import com.autonavi.minimap.MapStatic;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


public class CheckNetWork {
public static final String CTWAP = "ctwap";
public static final String CMWAP = "cmwap";
public static final String WAP_3G = "3gwap";
public static final String UNIWAP = "uniwap";
public static final int TYPE_NET_WORK_DISABLED = 0;// 网络不可用
public static final int TYPE_CM_CU_WAP = 1;// 移动联通wap10.0.0.172
public static final int TYPE_CT_WAP = 2;// 电信wap 10.0.0.200
public static final int TYPE_OTHER_NET = -1;// 电信,移动,联通,wifi 等net网络
public static final int TYPE_WIFI = 3;
public static Uri PREFERRED_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");


private Activity mActivity;
private Handler mHandler;


public CheckNetWork(Activity acitvity, Handler handler) {
mActivity = acitvity;
mHandler = handler;
}


public void start() {
mHandler.post(new Runnable() {


@Override
public void run() {
doCheck();
}
});


}


public void doCheck() {
int netWorkType = checkNetWorkType(mActivity);
if (netWorkType == TYPE_NET_WORK_DISABLED) {
new AlertDialog.Builder(mActivity).setTitle(R.string.show_new_tips)
.setMessage(R.string.check_net_work).setNeutralButton(
R.string.alert_tip,
new DialogInterface.OnClickListener() {


public void onClick(DialogInterface dialog,
int which) {
Intent it = new Intent(
"android.settings.SETTINGS");
mActivity.startActivity(it);
doExit();
// showUpgrade();
}
}).setNegativeButton(R.string.Cancel,
new DialogInterface.OnClickListener() {


public void onClick(DialogInterface dialog,
int which) {
MapStatic.finishApp();
}
}).setPositiveButton(R.string.check_retry,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// showUpgrade();
doExit();
}
}).show();
} else if (netWorkType == TYPE_OTHER_NET) {
Toast
.makeText(mActivity, R.string.check_net_work_net,
Toast.LENGTH_SHORT).show();
doExit();
} else if (netWorkType == TYPE_CM_CU_WAP || netWorkType == TYPE_CT_WAP) {
Toast.makeText(mActivity,R.string.check_net_work_wap,
Toast.LENGTH_SHORT).show();
doExit();
} else if (netWorkType == TYPE_WIFI) {
Toast.makeText(mActivity, R.string.check_net_work_wifi,
Toast.LENGTH_SHORT).show();
doExit();
}
}


private void doExit() {
Message msg = new Message();
msg.what = MapHelper.IS_CHECK_NET_WORK;
mHandler.sendMessage(msg);
}


/***
* 判断Network具体类型(联通移动wap,电信wap,其他net)

* */
private static int checkNetWorkType(Context mContext) {
try {


ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfoActivity = connectivityManager
.getActiveNetworkInfo();
if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {
// 注意一:
// NetworkInfo 为空或者不可以用的时候正常情况应该是当前没有可用网络,
// 但是有些电信机器,仍可以正常联网,
// 所以当成net网络处理依然尝试连接网络。
// (然后在socket中捕捉异常,进行二次判断与用户提示)。
Log.i("", "=====================>无网络");
return TYPE_NET_WORK_DISABLED;
} else {
// NetworkInfo不为null开始判断是网络类型
int netType = mobNetInfoActivity.getType();
if (netType == ConnectivityManager.TYPE_WIFI) {
// wifi net处理
Log.i("", "=====================>wifi网络");
return TYPE_WIFI;
} else if (netType == ConnectivityManager.TYPE_MOBILE) {
// 注意二:
// 判断是否电信wap:
// 不要通过getExtraInfo获取接入点名称来判断类型,
// 因为通过目前电信多种机型测试发现接入点名称大都为#777或者null,
// 电信机器wap接入点中要比移动联通wap接入点多设置一个用户名和密码,
// 所以可以通过这个进行判断!
final Cursor c = mContext.getContentResolver().query(
PREFERRED_APN_URI, null, null, null, null);
if (c != null) {
c.moveToFirst();
final String user = c.getString(c
.getColumnIndex("user"));
if (!TextUtils.isEmpty(user)) {
Log.i("", "=====================>代理:"
+ c.getString(c.getColumnIndex("proxy")));
if (user.startsWith(CTWAP)) {
Log.i("", "=====================>电信wap网络");
return TYPE_CT_WAP;
}
}
}
c.close();
// 注意三:
// 判断是移动联通wap:
// 其实还有一种方法通过getString(c.getColumnIndex("proxy")获取代理ip
// 来判断接入点,10.0.0.172就是移动联通wap,10.0.0.200就是电信wap,但在
// 实际开发中并不是所有机器都能获取到接入点代理信息,例如魅族M9 (2.2)等...
// 所以采用getExtraInfo获取接入点名字进行判断
String netMode = mobNetInfoActivity.getExtraInfo();
Log.i("", "netMode ================== " + netMode);
if (netMode != null) {
// 通过apn名称判断是否是联通和移动wap
netMode = netMode.toLowerCase();
if (netMode.equals(CMWAP) || netMode.equals(WAP_3G)
|| netMode.equals(UNIWAP)) {
Log.i("", "=====================>移动联通wap网络");
return TYPE_CM_CU_WAP;
}


}


}
}
} catch (Exception e) {
e.printStackTrace();
}


return TYPE_OTHER_NET;


}
}

你可能感兴趣的:(网络,String,null,dialog,电信,WAP)