android 通话录音功能

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

一、监听服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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(); 
      
    
  }  
}

  1. 二、activity使用 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 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
    
  
   
}

三、布局配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<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>