歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android電話信息相關API

Android平台提供的電話信息系統管理功能,主要包括:獲取電話信息(設備信息、SIM信息以及網絡信息)、偵聽電話狀態(呼叫狀態、服務狀態、信號強度狀態等)和調用電話撥號器。

TelephoneManager類

 在Manifest中添加Permission :<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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<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 未知網絡
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手機
PHONE_TYPE_CDMA   CDMA手機(2.1版本)
PHONE_TYPE_SIP         via SIP手機

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方法中調用即可實現撥打電話的功能,調用的是系統的撥號界面。

Copyright © Linux教程網 All Rights Reserved