Android来电监听

androidPhone来电自动接听电话录音自动回拨

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

 

[java][/java]
  1. public class MainActivity extends Activity {
  2.     @Override
  3.     protected void onCreate(Bundle savedInstanceState) {
  4.         // TODO Auto-generated method stub
  5.         super.onCreate(savedInstanceState);
  6.         TelephonyManager mTelephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  7.         mTelephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
  8.         ITelephony mTelephony = ITelephony.Stub.asInterface(ServiceManager.getService(“phone”));
  9.     }
  10.     class PhoneListener extends PhoneStateListener {
  11.         private MediaRecorder mRecorder = null;
  12.         private boolean flag = false;
  13.         private String phoneNum = null;
  14.         @Override
  15.         public void onCallStateChanged(int state, String incomingNumber) {
  16.             // TODO Auto-generated method stub
  17.             switch (state) {
  18.                 case TelephonyManager.CALL_STATE_IDLE:
  19.                     try {
  20.                         if (mRecorder != null) {
  21.                             mRecorder.stop(); // 停止
  22.                             mRecorder.release();// 释放
  23.                             mRecorder = null;// 垃圾回收
  24.                         }
  25.                         if (flag) {
  26.                             mTelephony.call(phoneNum);
  27.                             flag = false;
  28.                         }
  29.                     } catch (Exception e) {
  30.                         // TODO Auto-generated catch block
  31.                         e.printStackTrace();
  32.                     }
  33.                     break;
  34.                 case TelephonyManager.CALL_STATE_OFFHOOK:
  35.                     //通话录音
  36.                     try {
  37.                         mRecorder = new MediaRecorder();
  38.                         mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 此处只实现了录本地MIC输入的声音,若想录入对立通话者的声音
  39.                         mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  40.                         mRecorder.setOutputFile(“/mnt/sdcard/” + incomingNumber + “_”+ System.currentTimeMillis() + “.3gp”);
  41.                         mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  42.                         try {
  43.                             mRecorder.prepare();
  44.                         } catch (Exception e) {
  45.                             e.printStackTrace();
  46.                         }
  47.                         mRecorder.start();
  48.                         Thread.sleep(30 * 1000);
  49.                     } catch (Exception e) {
  50.                         // TODO Auto-generated catch block
  51.                         e.printStackTrace();
  52.                     }
  53.                     break;
  54.                 case TelephonyManager.CALL_STATE_RINGING:
  55.                     //来电自动接听
  56.                     try {
  57.                         phoneNum = incomingNumber;
  58.                         flag = true;
  59.                         if (mTelephony.isRinging()) {
  60.                             mTelephony.silenceRinger();
  61.                             mTelephony.answerRingingCall();
  62.                         }
  63.                     } catch (Exception e) {
  64.                         // TODO Auto-generated catch block
  65.                         e.printStackTrace();
  66.                     }
  67.                     break;
  68.                 default:
  69.                     break;
  70.             }
  71.             super.onCallStateChanged(state, incomingNumber);
  72.         }
  73.     }
  74. }

标签