在android里,第三方程序可以通过telephony对外的接口监听信号变化,电话状态变化等信息。最近在做项目的时候,把这个小串了下。
首先,先来看下telephony framework对外接口的组织架构。
其次,用个例子来解释下,第三方程序究竟是如何调用telephony api和监听telephony状态的。
packages/apps/Bluetooth/src/com/android/bluetooth/hfp/HeadsetPhoneState.java
class HeadsetPhoneState {
// ...
HeadsetPhoneState(Context context, HeadsetStateMachine stateMachine) {
// Cannot instantiate TelephonyManager directly; Retrieve a reference to an instance through
// Context.getSystemService(Context.TELEPHONY_SERVICE). Just like below.
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
void listenForPhoneState(boolean start) {
if (start) {
if (!mListening) {
// PhoneStateListener monitors changes in specific telephony states on the device.
// For example, Ser