android 通话录音功能,android录音功能,一、监听服务 p
android 通话录音功能,android录音功能,一、监听服务 p
一、监听服务
package com.newland.teleinterface; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.util.Calendar; import com.android.internal.telephony.ITelephony; import android.content.Context; import android.content.Intent; import android.media.MediaRecorder; import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; /** * * @author JD 功能:打电话,录音,通话时间 * */ public class TeleInterface { 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; } /** * 构造函数 * * @param activity * @param handler 自定义handler接收消息 msg.what 5:电话拨通时间 6:电话挂断时间 */ public TeleInterface(Context activity, Handler handler) { 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);// 注册监听器 telephonyManager.listen(new PhoneListener(), 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 MediaRecorder mediaRecorder; private File root_file, file; @Override public void onCallStateChanged(int state, String incomingNumber) { try { switch (state) { case TelephonyManager.CALL_STATE_RINGING: // 来电 this.incomeNumber = incomingNumber; break; case TelephonyManager.CALL_STATE_OFFHOOK: // 接通电话 calendar = Calendar.getInstance(); teleStartTime = calendar.getTime().toString(); String root_directory = Environment.getExternalStorageDirectory() + "/recorded_calls"; root_file = new File(root_directory); if (!root_file.exists()) { root_file.mkdir(); } if(this.incomeNumber==null){ this.incomeNumber = "call"; } String record_call = root_directory + "/" + this.incomeNumber + "_" + System.currentTimeMillis() + ".amr"; file = new File(record_call); if (!file.exists()) { file.createNewFile(); } mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); // 获得声音数据源 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // 格式输出 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setOutputFile(file.getAbsolutePath()); // 输出文件 mediaRecorder.prepare(); // 准备 mediaRecorder.start(); Log.d(TAG, "开始录音!"); Message msg_start = new Message(); msg_start.what = TELE_START_TIME; msg_start.obj = teleStartTime; Log.d(TAG, "StartTime=====" + teleStartTime); handler.sendMessage(msg_start); break; case TelephonyManager.CALL_STATE_IDLE: // 挂掉电话 if (mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; calendar = Calendar.getInstance(); teleEndTime = calendar.getTime().toString(); Message msg_end = new Message(); msg_end.what = TELE_END_TIME; msg_end.obj = teleEndTime; Log.d(TAG, "EndTime=====" + teleEndTime); handler.sendMessage(msg_end); } Log.d(TAG, "结束录音!"); break; } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
二、activity使用 ```javaimport android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView teleStartTime, teleEndTime;
private Button teleButton;
private TeleInterface teleInterface;
private TeleHandler teleHandler;
private EditText telNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
teleHandler = new TeleHandler(this, teleStartTime, teleEndTime);
teleInterface = new TeleInterface(this, teleHandler);
teleInterface.teleListen();
}
private void initView() {
teleStartTime = (TextView) findViewById(R.id.teleStartTime);
teleEndTime = (TextView) findViewById(R.id.teleEndTime);
teleButton = (Button) findViewById(R.id.teleButton);
telNo = (EditText) findViewById(R.id.telno);
teleButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.teleButton:
if(!telNo.getText().toString().trim().equals("")){
teleInterface.Call(telNo.getText().toString().trim());
}else{
teleInterface.Call("10010");
}
try {
// Thread.sleep(5000);
// teleInterface.sendDTMF("1".toCharArray()[0]);
teleInterface.endCall();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default: break; }
}
}
三、布局配置文件```xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/telno" android:numeric="integer" android:hint="电话号码"/> <Button android:id="@+id/teleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拨号" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通话时间:" /> <TextView android:id="@+id/teleStartTime" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结束时间:" /> <TextView android:id="@+id/teleEndTime" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
用户点评