zoukankan      html  css  js  c++  java
  • Android来电监听

    本文实现来电自动接听,自动录音,自动回拨功能。

    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		TelephonyManager mTelephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    		mTelephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
    		ITelephony mTelephony = ITelephony.Stub.asInterface(ServiceManager.getService("phone"));
    	}
    
    	class PhoneListener extends PhoneStateListener {
    		private MediaRecorder mRecorder = null;
    		private boolean flag = false;
    		private String phoneNum = null;
    		@Override
    		public void onCallStateChanged(int state, String incomingNumber) {
    			// TODO Auto-generated method stub
    			switch (state) {
    				case TelephonyManager.CALL_STATE_IDLE:
    					try {
    						if (mRecorder != null) {
    							mRecorder.stop(); // 停止
    							mRecorder.release();// 释放
    							mRecorder = null;// 垃圾回收
    						}
    						if (flag) {
    							mTelephony.call(phoneNum);
    							flag = false;
    						}	
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					break;
    				case TelephonyManager.CALL_STATE_OFFHOOK:
    					//通话录音
    					try {
    						mRecorder = new MediaRecorder();
    						mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 此处只实现了录本地MIC输入的声音,若想录入对立通话者的声音
    						mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    						mRecorder.setOutputFile("/mnt/sdcard/" + incomingNumber + "_"+ System.currentTimeMillis() + ".3gp");
    						mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    						try {
    							mRecorder.prepare();
    						} catch (Exception e) {
    							e.printStackTrace();
    						}
    						mRecorder.start();
    						Thread.sleep(30 * 1000);
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					break;
    				case TelephonyManager.CALL_STATE_RINGING:
    					//来电自动接听
    					try {
    						phoneNum = incomingNumber;
    						flag = true;
    						if (mTelephony.isRinging()) {
    							mTelephony.silenceRinger();
    							mTelephony.answerRingingCall();
    						}
    					} catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					break;
    				default:
    					break;
    			}
    			super.onCallStateChanged(state, incomingNumber);
    		}
    	}
    }
  • 相关阅读:
    洛谷P1120信息奥赛一本通1442 小木棍
    洛谷P1378 油滴扩展
    洛谷P1156 垃圾陷阱
    mybatis-Plus 实践篇之逆向工程
    Interceptor的使用及探究
    mysql,oracle,sqlServer 元数据查询
    navicat premium15免费版安装说明(附工具)
    打印日志你真的会吗?
    线程基础知识-必知必会
    空间复杂度&时间复杂度
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3301837.html
Copyright © 2011-2022 走看看