---
title: "Android电话信息相关API"
author: "喧嚣求静"
date: "2011-12-13T09:59:20Z"
tags: []
categories: ["移动开发"]
slug: "android-api"
draft: false
lastmod: "2011-12-13T10:22:26Z"
id: 1308272
url: "https://913.iteye.com/blog/1308272"
---

转载自：[http://www.linuxidc.com/Linux/2011-10/45049.htm](http://www.linuxidc.com/Linux/2011-10/45049.htm)

a[ndroid](http://www.linuxidc.com/topicnews.aspx?tid=11 "Android")平台提供的电话信息系统管理功能，主要包括：获取电话信息（设备信息、SIM信息以及网络信息）、侦听电话状态（呼叫状态、服务状态、信号强度状态等）和调用电话拨号器。

## TelephoneManager类

 在Manifest中添加Permission ：&lt;uses-permission [Android](http://www.linuxidc.com/topicnews.aspx?tid=11 "Android"):name="android.permission.READ\_PHONE\_STATE" /&gt;

 

 

TelephonyManager的获得：TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY\_SERVICE);

 

### Constants

 

String

ACTION\_PHONE\_STATE\_CHANGED

Broadcast intent action indicating that the call state (cellular) on the device has changed.

int

CALL\_STATE\_IDLE

Device call state: No activity.

空闲（无呼入或已挂机）

int

CALL\_STATE\_OFFHOOK

Device call state: Off-hook.

摘机（有呼入）

int

CALL\_STATE\_RINGING

Device call state: Ringing.

响铃（接听中）

int

DATA\_ACTIVITY\_DORMANT

Data connection is active, but physical link is down

电话数据活动状态类型：睡眠模式（3.1版本）

int

DATA\_ACTIVITY\_IN

Data connection activity: Currently receiving IP PPP traffic.

电话数据活动状态类型：数据流入

int

DATA\_ACTIVITY\_INOUT

Data connection activity: Currently both sending and receiving IP PPP traffic.

电话数据活动状态类型：数据交互

int

DATA\_ACTIVITY\_NONE

Data connection activity: No traffic.

电话数据活动状态类型：无数据流动

int

DATA\_ACTIVITY\_OUT

Data connection activity: Currently sending IP PPP traffic.

电话数据活动状态类型：数据流出

int

DATA\_CONNECTED

Data connection state: Connected.

数据连接状态类型：已连接

int

DATA\_CONNECTING

Data connection state: Currently setting up a data connection.

数据连接状态类型：正在连接

int

DATA\_DISCONNECTED

Data connection state: Disconnected.

数据连接状态类型：断开

int

DATA\_SUSPENDED

Data connection state: Suspended.

数据连接状态类型：已暂停

String

EXTRA\_INCOMING\_NUMBER

The lookup key used with the ACTION\_PHONE\_STATE\_CHANGED broadcast for a String containing the incoming phone number.

String

EXTRA\_STATE

The lookup key used with the ACTION\_PHONE\_STATE\_CHANGED broadcast for a String containing the new call state.

int

NETWORK\_TYPE\_1xRTT

Current network is 1xRTT

int

NETWORK\_TYPE\_CDMA

Current network is CDMA: Either IS95A or IS95B

int

NETWORK\_TYPE\_EDGE

Current network is EDGE

int

NETWORK\_TYPE\_EHRPD

Current network is eHRPD

int

NETWORK\_TYPE\_EVDO\_0

Current network is EVDO revision 0

int

NETWORK\_TYPE\_EVDO\_A

Current network is EVDO revision A

int

NETWORK\_TYPE\_EVDO\_B

Current network is EVDO revision B

int

NETWORK\_TYPE\_GPRS

Current network is GPRS

int

NETWORK\_TYPE\_HSDPA

Current network is HSDPA

int

NETWORK\_TYPE\_HSPA

Current network is HSPA

int

NETWORK\_TYPE\_HSPAP

Current network is HSPA+

int

NETWORK\_TYPE\_HSUPA

Current network is HSUPA

int

NETWORK\_TYPE\_IDEN

Current network is iDen

int

NETWORK\_TYPE\_LTE

Current network is LTE

int

NETWORK\_TYPE\_UMTS

Current network is UMTS

int

NETWORK\_TYPE\_UNKNOWN

Network type is unknown

int

PHONE\_TYPE\_CDMA

Phone radio is CDMA.

int

PHONE\_TYPE\_GSM

Phone radio is GSM.

int

PHONE\_TYPE\_NONE

No phone radio.

int

PHONE\_TYPE\_SIP

Phone is via SIP.

int

SIM\_STATE\_ABSENT

SIM card state: no SIM card is available in the device

int

SIM\_STATE\_NETWORK\_LOCKED

SIM card state: Locked: requries a network PIN to unlock

int

SIM\_STATE\_PIN\_REQUIRED

SIM card state: Locked: requires the user's SIM PIN to unlock

int

SIM\_STATE\_PUK\_REQUIRED

SIM card state: Locked: requires the user's SIM PUK to unlock

int

SIM\_STATE\_READY

SIM card state: Ready

int

SIM\_STATE\_UNKNOWN

SIM card state: Unknown.

 

 

### Fields

 

public static final String

EXTRA\_STATE\_IDLE

Value used with EXTRA\_STATE corresponding to CALL\_STATE\_IDLE.

public static final String

EXTRA\_STATE\_OFFHOOK

Value used with EXTRA\_STATE corresponding to CALL\_STATE\_OFFHOOK.

public static final String

EXTRA\_STATE\_RINGING

Value used with EXTRA\_STATE corresponding to CALL\_STATE\_RINGING.

 

 

### Public Methods

 

int

getCallState()

Returns a constant indicating the call state (cellular) on the device.

CellLocation

getCellLocation()

Returns the current location of the device.

int

getDataActivity()

Returns a constant indicating the type of activity on a data connection (cellular).

处理侦测到的数据活动的改变事件。通过该函数，可以获取数据活动状态信息。

电话数据活动状态类型定义在TelephoneyManager类中。

DATA\_ACTIVITY\_NONE            无数据流动  
DATA\_ACTIVITY\_IN                    数据流入  
DATA\_ACTIVITY\_OUT                数据流出  
DATA\_ACTIVITY\_INOUT            数据交互  
DATA\_ACTIVITY\_DORMANT     睡眠模式（2.1版本）

int

getDataState()

Returns a constant indicating the current data connection state (cellular).

String

getDeviceId()

Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.

获取设备标识（IMEI）

String

getDeviceSoftwareVersion()

Returns the software version number for the device, for example, the IMEI/SV for GSM phones.

获得软件版本

String

getLine1Number()

Returns the phone number string for line 1, for example, the MSISDN for a GSM phone.

线路1的电话号码

List&lt;NeighboringCellInfo&gt;

getNeighboringCellInfo()

Returns the neighboring cell information of the device.

String

getNetworkCountryIso()

Returns the ISO country code equivalent of the current registered operator's MCC (Mobile Country Code).

获取网络的国家ISO代码

String

getNetworkOperator()

Returns the numeric name (MCC+MNC) of current registered operator.

获取SIM移动国家代码（MCC）和移动网络代码（MNC）

String

getNetworkOperatorName()

Returns the alphabetic name of current registered operator. 

获取服务提供商姓名（中国移动、中国联通等）

int

getNetworkType()

Returns a constant indicating the radio technology (network type) currently in use on the device for data transmission.

获取网络类型

NETWORK\_TYPE\_UNKNOWN　未知网络  
NETWORK\_TYPE\_GPRS     
NETWORK\_TYPE\_EDGE　通用分组无线服务（2.5G）  
NETWORK\_TYPE\_UMTS    全球移动通信系统（3G）  
NETWORK\_TYPE\_HSDPA   
NETWORK\_TYPE\_HSUPA  
NETWORK\_TYPE\_HSPA  
NETWORK\_TYPE\_CDMA         CDMA网络（2.1版本）  
NETWORK\_TYPE\_EVDO\_0     CDMA2000 EV-DO版本0（2.1版本）  
NETWORK\_TYPE\_EVDO\_A     CDMA2000 EV-DO版本A（2.1版本）  
NETWORK\_TYPE\_EVDO\_B     CDMA2000 EV-DO版本B（2.1版本）  
NETWORK\_TYPE\_1xRTT         CDMA2000 1xRTT（2.1版本）  
NETWORK\_TYPE\_IDEN  
NETWORK\_TYPE\_LTE  
NETWORK\_TYPE\_EHRPD  
NETWORK\_TYPE\_HSPAP

int

getPhoneType()

Returns a constant indicating the device phone type.

电话类型

PHONE\_TYPE\_NONE   未知  
PHONE\_TYPE\_GSM      GSM[手机](http://www.linuxidc.com/mobile/index.htm "手机")  
PHONE\_TYPE\_CDMA   CDMA[手机](http://www.linuxidc.com/mobile/index.htm "手机")（2.1版本）  
PHONE\_TYPE\_SIP         via SIP[手机](http://www.linuxidc.com/mobile/index.htm "手机")

String

getSimCountryIso()

Returns the ISO country code equivalent for the SIM provider's country code.

获取SIM卡中国家ISO代码

String

getSimOperator()

Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM.

获得SIM卡中移动国家代码（MCC）和移动网络代码（MNC）

String

getSimOperatorName()

Returns the Service Provider Name (SPN).

获取服务提供商姓名（中国移动、中国联通等）

String

getSimSerialNumber()

Returns the serial number of the SIM, if applicable.

SIM卡序列号

int

getSimState()

Returns a constant indicating the state of the device SIM card.

SIM卡状态

SIM\_STATE\_UNKNOWN           　       未知状态  
SIM\_STATE\_ABSENT                      　 未插卡  
SIM\_STATE\_PIN\_REQUIRED     　    需要PIN码，需要SIM卡PIN码解锁  
SIM\_STATE\_PUK\_REQUIRED       　需要PUK码，需要SIM卡PUK码解锁  
SIM\_STATE\_NETWORK\_LOCKED    网络被锁定，需要网络PIN解锁  
SIM\_STATE\_READY                         　 准备就绪

其中：PIN个人识别码　PUK个人解锁码

String

getSubscriberId()

Returns the unique subscriber ID, for example, the IMSI for a GSM phone.

获得客户标识（IMSI）

String

getVoiceMailAlphaTag()

Retrieves the alphabetic identifier associated with the voice mail number.

String

getVoiceMailNumber()

Returns the voice mail number.

boolean

hasIccCard()

 

boolean

isNetworkRoaming()

Returns true if the device is considered roaming on the current network, for GSM purposes.

void

listen(PhoneStateListener listener, int events)

Registers a listener object to receive notification of changes in specified telephony states.

侦听电话的呼叫状态。

电话管理服务接口支持的侦听类型在PhoneStateListener类中定义。

 

 

 

 

## PhoneStateListener类

### Constants

int

LISTEN\_CALL\_FORWARDING\_INDICATOR

Listen for changes to the call-forwarding indicator.

侦听呼叫转移指示器改变事件

int

LISTEN\_CALL\_STATE

Listen for changes to the device call state.

侦听呼叫状态改变事件

int

LISTEN\_CELL\_LOCATION

Listen for changes to the device's cell location. Note that this will result in frequent callbacks to the listener.

侦听设备位置改变事件

int

LISTEN\_DATA\_ACTIVITY

Listen for changes to the direction of data traffic on the data connection (cellular).

侦听数据连接的流向改变事件

int

LISTEN\_DATA\_CONNECTION\_STATE

Listen for changes to the data connection state (cellular).

侦听数据连接状态改变事件

int

LISTEN\_MESSAGE\_WAITING\_INDICATOR

Listen for changes to the message-waiting indicator.

侦听消息等待指示器改变事件

int

LISTEN\_NONE

Stop listening for updates.

停止侦听

int

LISTEN\_SERVICE\_STATE

Listen for changes to the network service state (cellular).

侦听网络服务状态

int

LISTEN\_SIGNAL\_STRENGTH

This constant is deprecated. by LISTEN\_SIGNAL\_STRENGTHS

侦听网络信号强度

int

LISTEN\_SIGNAL\_STRENGTHS

Listen for changes to the network signal strengths (cellular).

 

### Public Constructors

 PhoneStateListener()          

### Public Methods

 

void

onCallForwardingIndicatorChanged(boolean cfi)

Callback invoked when the call-forwarding indicator changes.

void

onCallStateChanged(int state, String incomingNumber)

Callback invoked when device call state changes.

处理侦测到的电话呼叫状态的改变事件。通过该回调事件，可以获取来电号码，而且可以获取电话呼叫状态。即用switch(state){case TelephoneManager.CALL\_STATE\_……}来判断

void

onCellLocationChanged(CellLocation location)

Callback invoked when device cell location changes.

void

onDataActivity(int direction)

Callback invoked when data activity state changes.

void

onDataConnectionStateChanged(int state)

Callback invoked when connection state changes.

void

onDataConnectionStateChanged(int state, int networkType)

same as above, but with the network type.

处理侦测到的数据连接状态的改变状态。通过该回调函数，可以获取数据连接状态信息。

数据连接类型

DATA\_DISCONNECTED    断开  
DATA\_CONNECTING         正在连接  
DATA\_CONNECTED          已连接  
DATA\_SUSPENDED           已暂停

void

onMessageWaitingIndicatorChanged(boolean mwi)

Callback invoked when the message-waiting indicator changes.

void

onServiceStateChanged(ServiceState serviceState)

Callback invoked when device service state changes.

处理侦测到的服务状态的改变事件。通过该回调函数可以获取服务状态信息。

电话服务状态类型定义在ServiceState类中。

void

onSignalStrengthChanged(int asu)

This method is deprecated. Use onSignalStrengthsChanged(SignalStrength)

处理侦测到的信号强度的改变事件。通过该回调函数，可以获取信号强度类型。

void

onSignalStrengthsChanged(SignalStrength signalStrength)

Callback invoked when network signal strengths changes.

 

 

## 电话拨号

1. Uri uri=Uri.parse("tel:"+电话号码);
2. Intent intent=**new** Intent(Intent.ACTION\_DIAL,uri);
3. startActivity(intent);

其中的电话号码可以通过EditText中输入获得数据，如editText.getText().toString().trim()来获得  
上面的代码可以放在Button的onClick方法中调用即可实现拨打电话的功能，调用的是系统的拨号界面