转载自:http://www.linuxidc.com/Linux/2011-10/45049.htm
android平台提供的电话信息系统管理功能,主要包括:获取电话信息(设备信息、SIM信息以及网络信息)、侦听电话状态(呼叫状态、服务状态、信号强度状态等)和调用电话拨号器。
在Manifest中添加Permission :<uses-permission Android:name="android.permission.READ_PHONE_STATE" />
TelephonyManager的获得:TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
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. |
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. |
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 无数据流动 |
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<NeighboringCellInfo> |
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 未知网络 |
int |
getPhoneType() |
Returns a constant indicating the device phone type. 电话类型 PHONE_TYPE_NONE 未知 |
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 未知状态 其中: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类中定义。 |
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). |
PhoneStateListener()
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 断开 |
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. |
其中的电话号码可以通过EditText中输入获得数据,如editText.getText().toString().trim()来获得
上面的代码可以放在Button的onClick方法中调用即可实现拨打电话的功能,调用的是系统的拨号界面