简易的android 通话录音实现【只能录制MIC的声音,不能录制对方的声音】
【摘要】
因受系统限制,只能录自已麦的声音,录不到对方的声音,可能需要改内核才能实现双向录音;
接通电话和挂断电话时,震动一下;
使用广播接收者实现自启动;
public class phoneService extends Service { @Override public IBinder onB...
使用广播接收者实现自启动;
-
public class phoneService extends Service {
-
-
@Override
-
public IBinder onBind(Intent arg0) {
-
// TODO Auto-generated method stub
-
return null;
-
}
-
-
@Override
-
public void onCreate() {
-
// TODO Auto-generated method stub
-
super.onCreate();
-
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
-
telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
-
}
-
-
private final class PhoneListener extends PhoneStateListener{
-
private MediaRecorder mediaRecorder;
-
private File file;
-
private Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
-
-
@Override
-
public void onCallStateChanged(int state, String incomingNumber) {
-
try {
-
-
switch (state){
-
case TelephonyManager.CALL_STATE_RINGING: //来电
-
break;
-
-
case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话
-
file = new File(Environment.getExternalStorageDirectory(), incomingNumber + System.currentTimeMillis() + ".3gp");
-
mediaRecorder = new MediaRecorder();
-
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
-
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
-
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
-
mediaRecorder.setOutputFile(file.getAbsolutePath());
-
mediaRecorder.prepare();
-
mediaRecorder.start();
-
Toast.makeText(getApplicationContext(), "电话已接通,开始录音。", Toast.LENGTH_SHORT).show();
-
//震动一下
-
vibrator.vibrate(100);
-
break;
-
-
case TelephonyManager.CALL_STATE_IDLE: //挂断电话
-
if (mediaRecorder != null){
-
mediaRecorder.stop();
-
mediaRecorder.release();
-
mediaRecorder = null;
-
Toast.makeText(getApplicationContext(), "电话已挂断,录音停止。", Toast.LENGTH_SHORT).show();
-
//震动一下
-
vibrator.vibrate(100);
-
}
-
break;
-
}
-
-
}catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
public class BroadcastReceiver extends android.content.BroadcastReceiver {
-
-
@Override
-
public void onReceive(Context arg0, Intent arg1) {
-
Intent service = new Intent(arg0, phoneService.class);
-
arg0.startService(service);
-
}
-
}
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
package="com.eboy.phoneListener"
-
android:versionCode="1"
-
android:versionName="1.0">
-
-
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" />
-
-
<application android:label="@string/app_name"
-
android:icon="@drawable/ic_launcher"
-
android:theme="@style/AppTheme">
-
-
<service android:name=".phoneService"></service>
-
<receiver android:name=".BroadcastReceiver">
-
<intent-filter >
-
<!-- 开机自启动 -->
-
<action android:name="android.intent.action.BOOT_COMPLETED"/>
-
</intent-filter>
-
</receiver>
-
-
</application>
-
-
<!-- 读取电话状态权限 -->
-
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
-
<!-- SD卡读写权限 -->
-
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
-
<!-- 录音权限 -->
-
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
-
<!-- 震动权限 -->
-
<uses-permission android:name="android.permission.VIBRATE"/>
-
-
</manifest>
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8767551
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)