Android编程获取网络连接方式及判断手机卡所属运营商的方法
作者:hshm20517 时间:2023-09-18 21:54:14
本文实例讲述了Android编程获取网络连接方式及判断手机卡所属运营商的方法。分享给大家供大家参考,具体如下:
问题:项目中写的网络模块,感觉有点乱:两套代码 --模拟器、真机,维护起来十分麻烦。
解决办法:代码自动去检查到那种网络环境,然后调用不同的联网方式。
查看了模拟器上默认的接入点:移动网络 -- APN = "internet"
1、通过获取apn的名称,来判断网络
// 获取Mobile网络下的cmwap、cmnet
private int getCurrentApnInUse() {
int type = NONET;
Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI,
new String[] { "_id", "apn", "type" }, null, null, null);
cursor.moveToFirst();
int counts = cursor.getCount();
if(counts != 0){//适配平板 * 3G模块情况
if (!cursor.isAfterLast()) {
String apn = cursor.getString(1);
//#777、ctnet 都是中国电信定制机接入点名称,中国电信的接入点:Net、Wap都采用Net即非代理方式联网即可
//internet 是模拟器上模拟接入点名称
if (apn.equalsIgnoreCase("cmnet") || apn.equalsIgnoreCase("3gnet") || apn.equalsIgnoreCase("uninet")
|| apn.equalsIgnoreCase("#777") || apn.equalsIgnoreCase("ctnet") || apn.equalsIgnoreCase("internet")) {
type = WIFIAndCMNET;
} else if (apn.equalsIgnoreCase("cmwap") || apn.equalsIgnoreCase("3gwap") || apn.equalsIgnoreCase("uniwap")) {
type = CMWAP;
}
}else{
//适配中国电信定制机,如海信EG968,上面方式获取的cursor为空,所以换种方式
Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);
c.moveToFirst();
String user=c.getString(c.getColumnIndex("user"));
if(user.equalsIgnoreCase("ctnet")){
type = WIFIAndCMNET;
}
c.close();
}
}else{
type = WIFIAndCMNET;//平板 * 3G,采用非代理方式上网
}
cursor.close();
return type;
}
2、直接获取代理参数:proxy 来判断是否为代理
/**
* MOBILE方式下获取当前的网络连接方式,代理或非代理
*
*/
public static String getCurrentApnInUse(Context context)
{
Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI, new String[] { "_id", "apn", "type", "proxy" }, null, null,
null);
cursor.moveToFirst();
if (cursor.isAfterLast())
{
String apn = cursor.getString(3);
if (apn == null)
{
apn = "";
}
}
return apn;
}
/**
* 获取手机卡类型,移动、联通、电信
*
*/
private static int getMobileType(Context context)
{
int type = 0;
TelephonyManager iPhoneManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String iNumeric = iPhoneManager.getSimOperator();
if (iNumeric.length() > 0)
{
if (iNumeric.equals("46000") || iNumeric.equals("46002"))
{
// 中国移动
}
else if (iNumeric.equals("46001"))
{
// 中国联通
}
else if (iNumeric.equals("46003"))
{
// 中国电信
}
}
}
希望本文所述对大家Android程序设计有所帮助。
标签:Android,网络连接,运营商
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java并发编程之线程中断
2022-04-28 02:02:44
![](https://img.aspxhome.com/file/2023/7/128227_0s.png)
C#使用NPOI将List数据导出到Excel文档
2022-12-18 12:28:09
![](https://img.aspxhome.com/file/2023/6/98176_0s.jpg)
Java与C++实现相同的MD5加密算法简单实例
2023-08-31 09:43:02
![](https://img.aspxhome.com/file/2023/2/60892_0s.jpg)
Struts2 文件上传进度条的实现实例代码
2023-04-20 11:13:59
![](https://img.aspxhome.com/file/2023/0/72620_0s.png)
你是不是这样写异常处理代码的呢?
2022-08-08 02:10:10
Java编写实现坦克大战小游戏
2023-11-24 09:15:34
![](https://img.aspxhome.com/file/2023/7/59777_0s.jpg)
C# Winform按钮中图片实现左图右字的效果实例
2022-05-01 12:51:41
![](https://img.aspxhome.com/file/2023/6/113716_0s.png)
jackson反序列化时如何忽略不需要的字段
2021-09-26 20:36:20
android实现图片裁剪的两种方法
2022-10-27 18:41:05
C# ping网络IP 实现网络状态检测的方法
2023-01-11 14:31:05
C#实现收发邮件功能
2021-09-20 19:40:46
java日期工具类实例分享
2023-05-17 17:50:40
C++深入细致探究二叉搜索树
2021-12-31 00:09:24
![](https://img.aspxhome.com/file/2023/8/117758_0s.png)
Java基于Socket实现HTTP下载客户端
2022-07-06 01:02:30
![](https://img.aspxhome.com/file/2023/7/114037_0s.png)
c# SendMail发送邮件实例代码
2023-03-13 11:17:21
JavaEE中用response向客户端输出中文数据乱码问题分析
2022-07-14 00:39:27
![](https://img.aspxhome.com/file/2023/8/122978_0s.png)
Mybatis在sqlite中无法读写byte[]类问题的解决办法
2021-08-06 21:06:07
Android基准配置文件Baseline Profile方案提升启动速度
2022-08-12 18:54:42
![](https://img.aspxhome.com/file/2023/9/91019_0s.jpg)
java解析XML Node与Element的区别(推荐)
2021-11-12 18:32:13
![](https://img.aspxhome.com/file/2023/8/67678_0s.jpg)
详解springboot项目带Tomcat和不带Tomcat的两种打包方式
2023-11-28 08:23:41