Android开发之获取手机SIM卡信息

时间:2021-06-02 21:05:20

TelephonyManager是一个管理手机通话状态、电话网络信息的服务类。该类提供了大量的getXxx(),方法获取电话网络的相关信息。

TelephonyManager类概述:


可用于訪问有关设备上的电话服务信息。

应用程序能够使用这个类的方法来确定电话服务和状态,以及訪问某些类型的用户信息。应用程序还能够注冊一个侦听器以接收的电话状态变化通知。

你不能直接实例化这个类;相反,你能够通过Context.getSystemService(Context.TELEPHONY_SERVICE)方法还获取初始化TelephonyManager实例。

须要注意的是訪问某些电话信息permission-protected。。你的应用程序应该获得訪问手机位置和状态的的一些权限。

TelephonyManager类提供的基本的方法:


 

Public Methods

int

getCallState()

返回一个常数,表示设备上的呼叫状态

CellLocation

getCellLocation()

返回设备的当前位置。

int

getDataActivity()

返回一个常数,表示活动的数据连接的类型。

int

getDataState()

返回一个常数表示当前数据连接状态

String

getDeviceId()

返回唯一的设备ID,比如,IMEI GSM和MEID
CDMA手机。

String

getDeviceSoftwareVersion()

返回设备的软件版本,比如,的IMEI
/ SV GSM手机。

String

getLine1Number()

返回1号线的电话号码,比如。MSISDN用于GSM电话。

List<NeighboringCellInfo>

getNeighboringCellInfo()

返回设备的相邻小区信息。

String

getNetworkCountryIso()

返回注冊的网络运营商的国家代码

String

getNetworkOperator()

返回的MCC +跨国公司的注冊网络运营商

String

getNetworkOperatorName()

返回注冊的网络运营商的名字

int

getNetworkType()

返回一个常数,表示眼下在设备上使用的无线电技术(网络类型)。

int

getPhoneType()

返回设备的类型(手机制式)。

String

getSimCountryIso()

返回SIM卡运营商的国家代码

String

getSimOperator()

返回MCC +跨国公司(移动国家代码+移动网络代码)的提供者的SIM卡。

String

getSimOperatorName()

返回服务提供者的名称(SPN)。

String

getSimSerialNumber()

返回SIM卡的序列号,假设适用的话。

int

getSimState()

返回一个常数表示SIM卡设备的状态。

String

getSubscriberId()

返回唯一的用户ID,比如,IMSI为GSM手机。

String

getVoiceMailAlphaTag()

检索与语音信箱号码相关的字母标识符。

String

getVoiceMailNumber()

返回语音信箱号码。

boolean

hasIccCard()

boolean

isNetworkRoaming()

返回true,假设该设备被觉得是漫游当前网络上,支持GSM目的。

void

listen(PhoneStateListener listener, int events)

注冊一个侦听器对象接收改变指定的电话状态的通知。


应用实例:

执行效果图:

Android开发之获取手机SIM卡信息

TelephonyStatus类:


package com.jph.telephonystatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/**
* Describe:</br>
* 获取Sim卡信息
* 本实例通过TelephonyManager类的对象的getXxx()
* 方法获取手机Sim卡信息。
* @author jph
* Date:2014.07.22
* */
public class TelephonyStatus extends Activity {
ListView listShow;
//创建一个tManager类的实例
TelephonyManager tManager;
//声明一个表示Sim卡状态名的数组
String []statusName=new String[]{};
//声明一个表示Sim卡状态值得集合
ArrayList<String>statusValue=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listShow=(ListView)findViewById(R.id.listShow);
//获取系统的tManager对象
tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//获取表示各种状态名的数组
statusName=getResources().getStringArray(R.array.statusName);
//获取表示sim卡状态的的数组
String simStatus[]=getResources().getStringArray(R.array.simStatus);
//获取表示手机类型的数组
String phoneType[]=getResources().getStringArray(R.array.phoneType);
//获取设备编号
statusValue.add(tManager.getDeviceId());
//获取设备类型
statusValue.add(phoneType[tManager.getPhoneType()]);
//获取软件版本号
statusValue.add(tManager.getDeviceSoftwareVersion()==null?"未知"
:tManager.getDeviceSoftwareVersion());
//获取设备当前位置
statusValue.add(tManager.getCellLocation()==null? "未知"
:tManager.getCellLocation().toString());
//获取设备呼叫状态
switch (tManager.getCallState()) {
case TelephonyManager.CALL_STATE_IDLE:
statusValue.add("空暇");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
statusValue.add("正在通话");
break;
case TelephonyManager.CALL_STATE_RINGING:
statusValue.add("等待接听");
break;
default:
break;
}
//获取电话号码
statusValue.add(tManager.getLine1Number());
//获取运营商的国家代码
statusValue.add(tManager.getNetworkCountryIso());
//获取运营商的名称
statusValue.add(tManager.getNetworkOperatorName());
//获取网络类型
statusValue.add(getNetworkType(tManager.getNetworkType()));
//获取SPN
statusValue.add(tManager.getSimOperatorName().equals("")?"未知"
:tManager.getSimOperatorName());
//获取SIM卡的序列号
statusValue.add(tManager.getSimSerialNumber());
//获取SIM卡状态
statusValue.add(simStatus[tManager.getSimState()]);
List<Map<String, Object>> listItems=new ArrayList<Map<String,Object>>();
// 遍历statusValues集合。将statusNames、statusValues
// 的数据封装到List<Map<String , String>>集合中
for (int i = 0; i < statusName.length; i++) {
Map<String, Object>listItem=new HashMap<String, Object>();
listItem.put("name",statusName[i]);
listItem.put("value",statusValue.get(i));
listItems.add(listItem);
}
SimpleAdapter adapter=new SimpleAdapter(this, listItems, R.layout.line,
new String[]{"name","value"},new int[]{R.id.txtName,R.id.txtValue});
//为listShow设置Adapter
listShow.setAdapter(adapter);
}
//获取手机网络类型
private String getNetworkType(int networkType) {
// TODO Auto-generated method stub
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "1xRTT";
case TelephonyManager.NETWORK_TYPE_CDMA:
return "CDMA";
case TelephonyManager.NETWORK_TYPE_EDGE:
return "EDGE";
case TelephonyManager.NETWORK_TYPE_EHRPD:
return "EHRPD";
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "EVDO_0";
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "EVDO_A";
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return "EVDO_B";
case TelephonyManager.NETWORK_TYPE_GPRS:
return "GPRS";
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "HSDPA";
case TelephonyManager.NETWORK_TYPE_HSPA:
return "HSPA";
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "HSPAP";
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "HSUPA";
case TelephonyManager.NETWORK_TYPE_IDEN:
return "IDEN";
case TelephonyManager.NETWORK_TYPE_LTE:
return "LTE";
case TelephonyManager.NETWORK_TYPE_UMTS:
return "UMTS";
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return "UNKNOWN";
default:
return "UNKNOWN";
}
}
}

array.xml


<?

xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 声明一个名为statusName的数组 -->
<string-array name="statusName">
<item>设备编号</item>
<item>手机制式</item>
<item>软件版本号</item>
<item>设备当前位置</item>
<item>设备呼叫状态</item>
<item>电话号码</item>
<item>运营商的国家代码</item>
<item>运营商的名称</item>
<item>网络类型</item>
<item>SPN</item>
<item>SIM卡的序列号</item>
<item>SIM卡状态</item>
</string-array>
<!-- 声明一个名为phoneType的数组 -->
<string-array name="phoneType">
<item>未知</item>
<item>GSM</item>
<item>CDMA</item>
</string-array>
<!-- 声明一个名为simSatus的数组 -->
<string-array name="simStatus">
<item>状态未知</item>
<item>无SIM卡</item>
<item>被PIN加锁</item>
<item>被PUK加锁</item>
<item>被NetWork PIN加锁</item>
<item>已准备好</item>
</string-array>
</resources>

AndroidManifest.xml


<!-- 加入訪问手机位置的权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 加入訪问手机状态的权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

关于TelephonyManager其他实际应用可參照:Android开发之监听手机来电