您的位置:首页 > 移动开发

如何监听Phone的状态,第三方App如何拨打/接听电话?

2014-12-01 17:32 423 查看
Android Version]
Android V2.3 (GB, GB2,GB3)

Android V4.0, V4.1 (ICS, ICS2, JB)

Android V4.2 (JB2,JB3,JB5)

[DESCRIPTION]

Android提供了类PhoneStateListener以方便非Phone进程时监听Phone的状态,如Phone State,Service State,Signal Strength,Call forward,VoiceMail Indicator,Cell Location等。
Android提供了Interface ITelephony来帮助第三方App实现拨打、接听、挂断电话等操作。
类PhoneStateListener的实现在文件:PhoneStateListener.java (frameworks\base\telephony\java\android\telephony)中
类ITelephony的实现在文件:PhoneInterfaceManager.java (packages\apps\Phone\src\com\android\phone)中
[SOLUTION]
监听Phone状态:
1、 import android.telephony.PhoneStateListener.
2、 实现一个继承自PhoneStateListener的类(内部类、匿名类、普通类均可),并实例化该类。
例如 PhoneStateListener myListener = new PhoneStateListener(){...};
对于需要监听的状态,重写相应的函数。例如:希望监听Phone的状态,则重写public void onCallStateChanged(int state, String inComingNumber);
其中各参数的含义请参考PhoneStateListener的注释。
3、 书写代码
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);//单卡
或 tm.listenGemini(myListener, PhoneStateListener.LISTEN_CALL_STATE, Phone.GEMINI_SIM_1);//双卡
第三方App控制电话行为
ITelephony phone = (ITelephony)ITelephony.Stub.asInterface(ServiceManager.getService("phone"));
拨打电话:phone.call (单卡), phone.callGemini
(双卡),其他的行为可参考PhoneInterfaceManager中的函数。

最后请注意:
PhoneStateListener需要在使用完后调用TelephonyManager.listen(myListener, PhoneStateListener.LISTEN_NONE)来释放引用,否则会引起OOM问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: