电话录音

public class RecorderService extends Service {
	private MediaRecorder recorder;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		//拿到电话管理器
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		//监听电话状态
		//events:决定PhoneStateListener侦听什么内容
		tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
	}
	
	class MyListener extends PhoneStateListener{

	

		//一旦电话状态改变,此方法调用
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			super.onCallStateChanged(state, incomingNumber);
			
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				System.out.println("空闲");
				if(recorder != null){
					recorder.stop();
					recorder.release();
					recorder = null;
				}
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				System.out.println("响铃");
				if(recorder == null){
					recorder = new MediaRecorder();
					recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
					recorder.setOutputFile("sdcard/luyin.3gp");
					recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
					try {
						recorder.prepare();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				System.out.println("摘机");
				//开始录音
				if(recorder != null){
					recorder.start();
				}
				break;

			}
		}
		
	}
	
}




<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


阅读更多
文章标签: Android
个人分类: android
上一篇有序广播
下一篇service
想对作者说点什么? 我来说一句

Android电话录音监听Demo源码

2015年09月06日 4.54MB 下载

步步高最新录音电话软件

2018年03月09日 31.08MB 下载

没有更多推荐了,返回首页

关闭
关闭