关闭

Android中TelephonyMnager的使用

599人阅读 评论(0) 收藏 举报
分类:

TelephonyMnager的工作以及获取手机信息所需要的权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
//获取TelephonyMnager对象

TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

//获取设备id

String deviceId = tm.getDeviceId();

//获取SIM卡序列号

String SIMSerialNumber=tm.getSimSerialNumber();

//获得网络国家ISO编码

String networkCountryISO=tm.getNetworkCountryIso();

//获取SIM国家ISO代码
String SIMCountryISO=tm.getSimCountryIso();

//获取该设备的软件版本

String softwareVersion=tm.getDeviceSoftwareVersion()

//获取语音信箱号码
String voiceMailNumber=tm.getVoiceMailNumber();

获取手机类型CDMA/ GSM/无

int phoneType=tm.getPhoneType();

switch (phoneType) 
{
    case (TelephonyManager.PHONE_TYPE_CDMA):
               // your code
                   break;
    case (TelephonyManager.PHONE_TYPE_GSM) 
               // your code                 
                   break;
    case (TelephonyManager.PHONE_TYPE_NONE):
               // your code              
                    break;
 }

判断手机是否在漫游,如果漫游返回true

boolean isRoaming=tm.isNetworkRoaming();
if(isRoaming)
      phoneDetails+="\nIs In Roaming : "+"YES";
else
     phoneDetails+="\nIs In Roaming : "+"NO";

获取SIM卡的状态

int SIMState=tm.getSimState();
switch(SIMState)
{
    case TelephonyManager.SIM_STATE_ABSENT :
        // your code
        break;
    case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
        // your code
        break;
    case TelephonyManager.SIM_STATE_PIN_REQUIRED :
        // your code
        break;
    case TelephonyManager.SIM_STATE_PUK_REQUIRED :
        // your code
        break;
    case TelephonyManager.SIM_STATE_READY :
        // your code
        break;
    case TelephonyManager.SIM_STATE_UNKNOWN :
        // your code
        break;

}

获取网络信息

// Get connected network country ISO code
String networkCountry = telephonyManager.getNetworkCountryIso();

// Get the connected network operator ID (MCC + MNC)
String networkOperatorId = telephonyManager.getNetworkOperator();

// Get the connected network operator name
String networkName = telephonyManager.getNetworkOperatorName();

// Get the type of network you are connected with 
int networkType = telephonyManager.getNetworkType();
switch (networkType) {
case (TelephonyManager.NETWORK_TYPE_1xRTT) :"  Your Code ":
break;
case (TelephonyManager.NETWORK_TYPE_CDMA) :"  Your Code ":
break;
case (TelephonyManager.NETWORK_TYPE_EDGE) : "  Your Code ":
break;
case (TelephonyManager.NETWORK_TYPE_EVDO_0) :"  Your Code ":
break;

获取SIM卡的信息

int simState = telephonyManager.getSimState();
switch (simState) 
{
   case (TelephonyManager.SIM_STATE_ABSENT): break;
   case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): break;
   case (TelephonyManager.SIM_STATE_PIN_REQUIRED): break;
   case (TelephonyManager.SIM_STATE_PUK_REQUIRED): break;
   case (TelephonyManager.SIM_STATE_UNKNOWN): break;
   case (TelephonyManager.SIM_STATE_READY): 
    {
        // Get the SIM country ISO code
        String simCountry = telephonyManager.getSimCountryIso();
        // Get the operator code of the active SIM (MCC + MNC)
        String simOperatorCode = telephonyManager.getSimOperator();
        // Get the name of the SIM operator
        String simOperatorName = telephonyManager.getSimOperatorName();
        // -- Requires READ_PHONE_STATE uses-permission --
        // Get the SIM’s serial number
        String simSerial = telephonyManager.getSimSerialNumber();
  }
}

翻译文章:Using Telephony Manager In Android with Example

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:413260次
    • 积分:7118
    • 等级:
    • 排名:第3530名
    • 原创:264篇
    • 转载:31篇
    • 译文:22篇
    • 评论:58条
    成长轨迹
    目前研究方向:Android,React Native,机器学习

      微信公众号:DroidMind
      (欢迎关注)