电话窃听器

package com.example.app05_phonerecorder;


import java.io.IOException;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


public class RecordService extends Service{


@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
//创建一个电话管理器对象
TelephonyManager manager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
//创建电话状态的一个监听器
MyPhoneStateListener listener = new MyPhoneStateListener();
//调用电话管理器的listen方法注册监听
//参数:1.监听器,2.要监听打电话的状态
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener{
private MediaRecorder recorder;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.e("TAG", "空闲状态"+"录音结束");
if (recorder != null) {
try {
recorder.stop();//停止录音
} catch (Exception e) {
e.printStackTrace();
}finally{
recorder.reset();   //重置
recorder.release(); //释放
}


}

break;
case TelephonyManager.CALL_STATE_RINGING:
Log.e("TAG","响铃"+incomingNumber+",准备一个录音机");
recorder = new MediaRecorder();


//设置音频的输入源  MIC只能录自己的声音 ;VOICE_CALL可以录双方的声音
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置音频输出的格式.3gp(体积小 ,效果差 ,压缩狠)
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置音频的编码,AMR是早期彩屏彩铃手机上使用的音频格式,一般用做手机铃声
//NB(窄带)   WB(宽带)
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置输入之后文件保存的路径
recorder.setOutputFile(getCacheDir()+"/"+incomingNumber+".3gp");
//录音机开始准备
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("TAG","接电话了"+incomingNumber + ",开始录音");
recorder.start();   // 开始录音




default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}


}

} 电话窃听器_第1张图片 电话窃听器_第2张图片

你可能感兴趣的:(电话窃听器)