第三方程序调用Android Telephony 接口机制

本文探讨第三方应用如何在Android系统中通过Telephony接口监听信号强度和电话状态变化。内容涉及telephony framework的架构,并通过一个Bluetooth应用的示例说明调用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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, Service state, signal strength, message waiting indicator (voicemail),
                // and others.
                mTelephonyManager.listen(mPhoneStateListener,
                                         PhoneStateListener.LISTEN_SERVICE_STATE |
                                         PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
                mListening = true;
            }
        } else {
            if (mListening) {
                mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
                mListening = false;
            }
        }
    }
    //...

    // mPhoneStateListener overrides PhoneStateListener
    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
        @Override
        public void onServiceStateChanged(ServiceState serviceState) {
            mServiceState = serviceState;
            mService = (serviceState.getState() == ServiceState.STATE_IN_SERVICE) ?
                HeadsetHalConstants.NETWORK_STATE_AVAILABLE :
                HeadsetHalConstants.NETWORK_STATE_NOT_AVAILABLE;
            setRoam(serviceState.getRoaming() ? HeadsetHalConstants.SERVICE_TYPE_ROAMING
                                              : HeadsetHalConstants.SERVICE_TYPE_HOME);
            sendDeviceStateChanged();
        }

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            int prevSignal = mSignal;
            if (signalStrength.isGsm()) {
                mSignal = gsmAsuToSignal(signalStrength);
            } else {
                mSignal = cdmaDbmEcioToSignal(signalStrength);
            }
            // network signal strength is scaled to BT 1-5 levels.
            // This results in a lot of duplicate messages, hence this check
            if (prevSignal != mSignal)
                sendDeviceStateChanged();
        }
        //...
    };
    //...
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值