Created
April 15, 2017 14:17
-
-
Save lgyjg/381b48734a6b8d9fd62fa00898d54d4c to your computer and use it in GitHub Desktop.
获取手机号码\获取手机运营商
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SIMCardInfo { | |
/** | |
* TelephonyManager提供设备上获取通讯服务信息的入口。 应用程序可以使用这个类方法确定的电信服务商和国家以及某些类型的用户访问信息。 | |
* 应用程序也可以注册一个监听器到电话收状态的变化。不需要直接实例化这个类 | |
* 使用Context.getSystemService(Context.TELEPHONY_SERVICE)来获取这个类的实例。 | |
*/ | |
private TelephonyManager telephonyManager; | |
/** | |
* 国际移动用户识别码 | |
*/ | |
private String IMSI; | |
public SIMCardInfo(Context context) { | |
telephonyManager = (TelephonyManager) context | |
.getSystemService(Context.TELEPHONY_SERVICE); | |
} | |
/** | |
* Role:获取当前设置的电话号码 <BR> | |
*/ | |
public String getNativePhoneNumber() { | |
String NativePhoneNumber = null; | |
NativePhoneNumber = telephonyManager.getLine1Number(); | |
return telephonyManager.getLine1Number(); | |
} | |
/** | |
* Role:Telecom service providers获取手机服务商信息 <BR> | |
* 需要加入权限<uses-permission | |
* android:name="android.permission.READ_PHONE_STATE"/> <BR> | |
*/ | |
public String getProvidersName() { | |
String ProvidersName = null; | |
// 返回唯一的用户ID;就是这张卡的编号神马的 | |
IMSI = telephonyManager.getSubscriberId(); | |
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。 | |
if(IMSI == null || "".equals(IMSI)){ | |
ProvidersName = "获取手机号码失败"; | |
}else | |
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) { | |
ProvidersName = "中国移动"; | |
} else if (IMSI.startsWith("46001")) { | |
ProvidersName = "中国联通"; | |
} else if (IMSI.startsWith("46003")) { | |
ProvidersName = "中国电信"; | |
} | |
return ProvidersName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment