package com.coolape.tianrun; import java.util.Calendar; import com.android.internal.telephony.ITelephony; import com.newland.PhoneUtils; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Message; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; /** * * @author JD 功能:打电话,录音,通话时间 * */ public class CLTeleInterface { private String TAG = "TeleInterface"; private Context activity; // private Handler handler; private Calendar calendar; private String teleStartTime; private String teleEndTime; private TelephonyManager telephonyManager; public static int TELE_START_TIME = 5; public static int TELE_END_TIME = 6; public String getTeleStartTime() { return teleStartTime; } public String getTeleEndTime() { return teleEndTime; } public PhoneListener listener; /** * 构造函数 * * @param activity * @param handler * 自定义handler接收消息 msg.what 5:电话拨通时间 6:电话挂断时间 */ // public TeleInterface(Context activity, Handler handler) { public CLTeleInterface(Context activity) { this.activity = activity; // this.handler = handler; } /** * 拨打电话 * * @param phoneNum * 需要拨打号码 */ public void Call(String phoneNum) { if (phoneNum.length() != 0) { Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phoneNum)); activity.startActivity(phoneIntent); } else { Toast.makeText(activity, "不能输入为空", Toast.LENGTH_LONG).show(); } } /** * 来电监听注册 */ public void teleListen() { telephonyManager = (TelephonyManager) activity .getSystemService(Context.TELEPHONY_SERVICE);// 注册监听器 if(listener == null) { listener = new PhoneListener(); } telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);// 监听电话状态 } /** * 挂断电话 * * @throws Exception */ public void endCall() throws Exception { ITelephony iTelephony = PhoneUtils.getITelephony(telephonyManager); iTelephony.endCall();// 自动挂断电话 } private final class PhoneListener extends PhoneStateListener { private String incomeNumber=""; // 来电号码 private boolean isComingCall = false; // private MediaRecorder mediaRecorder; // private File root_file, file; @Override public void onCallStateChanged(int state, String incomingNumber) { try { switch (state) { case TelephonyManager.CALL_STATE_RINGING: // 来电 Log.d(TAG, "来电============"); this.incomeNumber = incomingNumber; Log.d(TAG, "incomingNumber==" + incomingNumber); isComingCall = true; break; case TelephonyManager.CALL_STATE_OFFHOOK: // 接通电话 Log.d(TAG, "接通电话============"); calendar = Calendar.getInstance(); teleStartTime = calendar.getTime().toString(); Message msg_start = new Message(); msg_start.what = TELE_START_TIME; msg_start.obj = teleStartTime; Log.d(TAG, "StartTime=====" + teleStartTime); if (U3dPlugin.isNeedRecordOutCall) { // isOutgoingCall = true; U3dPlugin.onBegainOutgoingCall(); } break; case TelephonyManager.CALL_STATE_IDLE: // 挂掉电话 if(isComingCall) { U3dPlugin.onEndincomeCall(incomeNumber); // incomeNumber = ""; isComingCall = false; } else { if (U3dPlugin.isNeedRecordOutCall) { U3dPlugin.onEndgoingCall(); // isOutgoingCall = false; } } Log.d(TAG, "挂掉电话===================!"); break; } super.onCallStateChanged(state, incomingNumber); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }