Android通讯:通话

Android通讯之通话功能的实现:

在Android中,android.telephony.TelephonyManager对象是开发者获取当前通话网络相关信息的窗口,通过TelephonyManager对象可以查看当前的通话状态,SIM卡的消息等相关内容:
// 获得TelephonyManager对象
TelephonyManager telManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// 获得通话网络类型信息
int phonyType = telManager.getCallState();
if(callstate == TelephonyManager.CALL_STATE_IDLE){
    ......//如果未在通话中,则处理相关事宜
}
......

此外,使用TelephonyManager.listen函数,可以注册android.telephony.PhoneStateListener对象来实时监听通话状态的变更情况,使得应用有机会对通话状态进行辅助处理。比如,利用PhoneStateListener对象可以监控来电信息,实时查询来电的归属地:
// 构造PhoneStateListener子类来处理相关事件
public class MyListener extends PhoneStateListener{
    public onCallStateChanged(int state,String incomingNumber){
    if(state == TelephonyManager.CALL_STATE_RINGING){
        ......//处理来电事件
    }
    }
}
......
// 打开监听电话状态的变更情况
TelephonyManager telManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(new MyListener(),PhoneStateListener.LISTEN_CALL_STATE);

处于安全性的考虑,Android并没有将拨号呼叫、接听电话等通话控制的接口暴露给开发者。如果在应用中需要打电话,则需要构造Intent对象调用通话应用来实现:
final Uri phone = Uri.parse("tel:1234567");
// 一种方式是调用拨号组件
startActivity(new Intent(Intent.ACTION_DIAL,phone));
//或者也可以直接拨号
startActivity(new Intent(Intent.ACTION_CALL,phone));

在Android中,通常会使用Intent.ACTION_DIAL构造Intent对象来发起拨号请求,以避免用户在不知情的情况下拨通而浪费通话费用。

例子:未接来电处理
实现思路 :
1、继承PhoneStateListener后,当手机的状态改变后将会触发onCallStateChanged。手机的状态分为CALL_STATE_RINGING(响铃中),CALL_STATE_IDLE(空闲),CALL_STATE_OFFHOOK(忙音)。
2、记录上一次的手机状态,如果手机现在空闲,上次状态响铃的话,就可以判断是未接来电。

实现步骤:
1、编写CallListener,处理手机状态变更监听,当状态改变时进行处理。
    public class CallListener extends PhoneStateListener {
        private static int lastetState = TelephonyManager.CALL_STATE_IDLE; // 最后的状态
        private Context context;
        public CallListener(Context context) {
                super();
                this.context = context;
        }
        public void onCallStateChanged(int state, String incomingNumber) {
                Log.v(TAG, "CallListener call state changed : " + incomingNumber);
                String m = null;
                // 如果当前状态为空闲,上次状态为响铃中的话,则破觚为认为是未接来电
                if(lastetState == TelephonyManager.CALL_STATE_RINGING && state == TelephonyManager.CALL_STATE_IDLE){
                        sendSmgWhenMissedCall(incomingNumber);
                }
                lastetState = state;// 最后的时候改变当前值
        }
        private void sendSmgWhenMissedCall(String incomingNumber) {
             // ... 进行未接来电处理(发短信、发email等等通知)
        }
    }

2、编写CallReceiver,注册来电广播接收器。
    public class CallReceiver extends BroadcastReceiver{
        public void onReceive(Context context, Intent intent) {
            Log.i("sms", "CallReceiver Start...");
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            CallListener customPhoneListener = new CallListener(context);
            telephony.listen(customPhoneListener,PhoneStateListener.LISTEN_CALL_STATE);
            Bundle bundle = intent.getExtras();
            String phoneNr = bundle.getString("incoming_number");
            Log.i("sms", "CallReceiver Phone Number : " + phoneNr);
        }
    }

3、在AndroidManifest.xml中的application节点下添加如下代码,进行注册电话状态改变广播接收.
<manifest ...>
  <application ...>
    <receiver android:name=".call.service.CallReceiver">
            <intent-filter android:priority="100">
                    <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
    </receiver>
  </application>
</manifest>

4、在AndroidManifest.xml中添加读取手机状态的权限.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>

示例代码下载链接:http://www.apkbus.com/android-137388-1-1.html

参考文章链接:

原文地址:https://www.cnblogs.com/klcf0220/p/3248056.html