大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > Android技巧 > 教你Android来去电通话自动录音的方法

教你Android来去电通话自动录音的方法(2)

关键词:Android通话录音  阅读(1714) 赞(16)

[摘要]本文是对Android来去电通话自动录音的方法的讲解,对学习Android编程技术有所帮助,与大家分享。
XML/HTML代码
  1. <uses-permissionandroid:name="android.permission.READ_LOGS"/>

然后,在读取日志的类中检测到去电各个状态的地方发送一个广播,那么,读取日志的完整代码如下:

Java代码
  1. packagecom.sdvdxl.phonerecorder;
  2. importjava.io.BufferedReader;
  3. importjava.io.IOException;
  4. importjava.io.InputStream;
  5. importjava.io.InputStreamReader;
  6. importcom.sdvdxl.outgoingcall.OutgoingCallState;
  7. importandroid.content.Context;
  8. importandroid.content.Intent;
  9. importandroid.util.Log;
  10. /**
  11. *
  12. *@authormrloong
  13. *找到日志中的
  14. *onPhoneStateChanged:mForegroundCall.getState()这个是前台呼叫状态
  15. *mBackgroundCall.getState()后台电话
  16. *若是DIALING则是正在拨号,等待建立连接,但对方还没有响铃,
  17. *ALERTING呼叫成功,即对方正在响铃,
  18. *若是ACTIVE则已经接通
  19. *若是DISCONNECTED则本号码呼叫已经挂断
  20. *若是IDLE则是处于空闲状态
  21. *
  22. */
  23. publicclassReadLogextendsThread{
  24. privateContextctx;
  25. privateintlogCount;
  26. privatestaticfinalStringTAG="LogInfoOutGoingCall";
  27. /**
  28. *前后台电话
  29. *@authorsdvdxl
  30. *
  31. */
  32. privatestaticclassCallViewState{
  33. publicstaticfinalStringFORE_GROUND_CALL_STATE="mForeground";
  34. }
  35. /**
  36. *呼叫状态
  37. *@authorsdvdxl
  38. *
  39. */
  40. privatestaticclassCallState{
  41. publicstaticfinalStringDIALING="DIALING";
  42. publicstaticfinalStringALERTING="ALERTING";
  43. publicstaticfinalStringACTIVE="ACTIVE";
  44. publicstaticfinalStringIDLE="IDLE";
  45. publicstaticfinalStringDISCONNECTED="DISCONNECTED";
  46. }
  47. publicReadLog(Contextctx){
  48. this.ctx=ctx;
  49. }
  50. /**
  51. *读取Log流
  52. *取得呼出状态的log
  53. *从而得到转换状态
  54. */
  55. @Override
  56. publicvoidrun(){
  57. Log.d(TAG,"开始读取日志记录");
  58. String[]catchParams={"logcat","InCallScreen*:s"};
  59. String[]clearParams={"logcat","-c"};
  60. try{
  61. Processprocess=Runtime.getRuntime().exec(catchParams);
  62. InputStreamis=process.getInputStream();
  63. BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
  64. Stringline=null;
  65. while((line=reader.readLine())!=null){
  66. logCount++;
  67. //输出所有
  68. Log.v(TAG,line);
  69. //日志超过512条就清理
  70. if(logCount>512){
  71. //清理日志
  72. Runtime.getRuntime().exec(clearParams)
  73. .destroy();//销毁进程,释放资源
  74. logCount=0;
  75. Log.v(TAG,"-----------清理日志---------------");
  76. }
  77. /*---------------------------------前台呼叫-----------------------*/
  78. //空闲
  79. if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
  80. &&line.contains(ReadLog.CallState.IDLE)){
  81. Log.d(TAG,ReadLog.CallState.IDLE);
  82. }
  83. //正在拨号,等待建立连接,即已拨号,但对方还没有响铃,
  84. if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
  85. &&line.contains(ReadLog.CallState.DIALING)){
  86. //发送广播
  87. IntentdialingIntent=newIntent();
  88. dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.DIALING);
  89. ctx.sendBroadcast(dialingIntent);
  90. Log.d(TAG,ReadLog.CallState.DIALING);
  91. }
  92. //呼叫对方正在响铃
  93. if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
  94. &&line.contains(ReadLog.CallState.ALERTING)){
  95. //发送广播
  96. IntentdialingIntent=newIntent();
  97. dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.ALERTING);
  98. ctx.sendBroadcast(dialingIntent);
  99. Log.d(TAG,ReadLog.CallState.ALERTING);
  100. }
  101. //已接通,通话建立
  102. if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
  103. &&line.contains(ReadLog.CallState.ACTIVE)){
  104. //发送广播
  105. IntentdialingIntent=newIntent();
  106. dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.ACTIVE);
  107. ctx.sendBroadcast(dialingIntent);
  108. Log.d(TAG,ReadLog.CallState.ACTIVE);
  109. }
  110. //断开连接,即挂机
  111. if(line.contains(ReadLog.CallViewState.FORE_GROUND_CALL_STATE)
  112. &&line.contains(ReadLog.CallState.DISCONNECTED)){
  113. //发送广播
  114. IntentdialingIntent=newIntent();
  115. dialingIntent.setAction(OutgoingCallState.ForeGroundCallState.DISCONNECTED);
  116. ctx.sendBroadcast(dialingIntent);
  117. Log.d(TAG,ReadLog.CallState.DISCONNECTED);
  118. }
  119. }//ENDwhile
  120. }catch(IOExceptione){
  121. e.printStackTrace();
  122. }//ENDtry-catch
  123. }//ENDrun
  124. }//ENDclassReadLog

发送了广播,那么就要有接收者,定义接收者如下(关于录音机的代码可以先忽略):

Java代码
  1. packagecom.sdvdxl.phonerecorder;
  2. importandroid.content.BroadcastReceiver;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. importandroid.util.Log;
  6. importcom.sdvdxl.outgoingcall.OutgoingCallState;
  7. publicclassOutgoingCallReciverextendsBroadcastReceiver{
  8. staticfinalStringTAG="Recorder";
  9. privateMyRecorderrecorder;
  10. publicOutgoingCallReciver(){
  11. recorder=newMyRecorder();
  12. }
  13. publicOutgoingCallReciver(MyRecorderrecorder){
  14. this.recorder=recorder;
  15. }
  16. @Override
  17. publicvoidonReceive(Contextctx,Intentintent){
  18. StringphoneState=intent.getAction();
  19. if(phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)){
  20. StringphoneNum=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);//拨出号码
  21. recorder.setPhoneNumber(phoneNum);
  22. recorder.setIsCommingNumber(false);
  23. Log.d(TAG,"设置为去电状态");
  24. Log.d(TAG,"去电状态呼叫:"+phoneNum);
  25. }
  26. if(phoneState.equals(OutgoingCallState.ForeGroundCallState.DIALING)){
  27. Log.d(TAG,"正在拨号...");
  28. }
  29. if(phoneState.equals(OutgoingCallState.ForeGroundCallState.ALERTING)){
  30. Log.d(TAG,"正在呼叫...");
  31. }
  32. if(phoneState.equals(OutgoingCallState.ForeGroundCallState.ACTIVE)){
  33. if(!recorder.isCommingNumber()&&!recorder.isStarted()){
  34. Log.d(TAG,"去电已接通启动录音机");
  35. recorder.start();
  36. }
  37. }
  38. if(phoneState.equals(OutgoingCallState.ForeGroundCallState.DISCONNECTED)){
  39. if(!recorder.isCommingNumber()&&recorder.isStarted()){
  40. Log.d(TAG,"已挂断关闭录音机");
  41. recorder.stop();
  42. }
  43. }
  44. }
  45. }

其中有这么一段代码:

Java代码
  1. StringphoneState=intent.getAction();
  2. if(phoneState.equals(Intent.ACTION_NEW_OUTGOING_CALL)){
  3. StringphoneNum=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);//拨出号码
  4. recorder.setPhoneNumber(phoneNum);
  5. recorder.setIsCommingNumber(false);
  6. Log.d(TAG,"设置为去电状态");
  7. Log.d(TAG,"去电状态呼叫:"+phoneNum);
  8. }

这里是接收系统发出的广播,用于接收去电广播。这样,就获得了去电状态。

3、有了以上主要代码,可以说,来去电监听功能算是完成了,下面创建一个service来运行监听:

Java代码
  1. packagecom.sdvdxl.service;
  2. importandroid.app.Service;
  3. importandroid.content.Context;
  4. importandroid.content.Intent;
  5. importandroid.content.IntentFilter;
  6. importandroid.os.IBinder;
  7. importandroid.telephony.PhoneStateListener;
  8. importandroid.telephony.TelephonyManager;
  9. importandroid.util.Log;
  10. importandroid.widget.Toast;
  11. importcom.sdvdxl.outgoingcall.OutgoingCallState;
  12. importcom.sdvdxl.phonerecorder.MyRecorder;
  13. importcom.sdvdxl.phonerecorder.OutgoingCallReciver;
  14. importcom.sdvdxl.phonerecorder.TelListener;
  15. publicclassPhoneCallStateServiceextendsService{
  16. privateOutgoingCallStateoutgoingCallState;
  17. privateOutgoingCallReciveroutgoingCallReciver;
  18. privateMyRecorderrecorder;
  19. @Override
  20. publicvoidonCreate(){
  21. super.onCreate();
  22. //------以下应放在onStartCommand中,但2.3.5以下版本不会因service重新启动而重新调用--------
  23. //监听电话状态,如果是打入且接听或者打出则开始自动录音
  24. //通话结束,保存文件到外部存储器上
  25. Log.d("Recorder","正在监听中...");
  26. recorder=newMyRecorder();
  27. outgoingCallState=newOutgoingCallState(this);
  28. outgoingCallReciver=newOutgoingCallReciver(recorder);
  29. outgoingCallState.startListen();
  30. Toast.makeText(this,"服务已启动",Toast.LENGTH_LONG).show();
  31. //去电
  32. IntentFilteroutgoingCallFilter=newIntentFilter();
  33. outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.IDLE);
  34. outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.DIALING);
  35. outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.ALERTING);
  36. outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.ACTIVE);
  37. outgoingCallFilter.addAction(OutgoingCallState.ForeGroundCallState.DISCONNECTED);
  38. outgoingCallFilter.addAction("android.intent.action.PHONE_STATE");
  39. outgoingCallFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
  40. //注册接收者
  41. registerReceiver(outgoingCallReciver,outgoingCallFilter);
  42. //来电
  43. TelephonyManagertelmgr=(TelephonyManager)getSystemService(
  44. Context.TELEPHONY_SERVICE);
  45. telmgr.listen(newTelListener(recorder),PhoneStateListener.LISTEN_CALL_STATE);
  46. }
  47. @Override
  48. publicIBinderonBind(Intentintent){
  49. //TODOAuto-generatedmethodstub
  50. returnnull;
  51. }
  52. @Override
  53. publicvoidonDestroy(){
  54. super.onDestroy();
  55. unregisterReceiver(outgoingCallReciver);
  56. Toast.makeText(
  57. this,"已关闭电话监听服务",Toast.LENGTH_LONG)
  58. .show();
  59. Log.d("Recorder","已关闭电话监听服务");
  60. }
  61. @Override
  62. publicintonStartCommand(Intentintent,intflags,intstartId){
  63. returnSTART_STICKY;
  64. }
  65. }

注册以下service:

XML/HTML代码
  1. <serviceandroid:name="com.sdvdxl.service.PhoneCallStateService"/>

到此为止,来去电状态的监听功能算是完成了,剩下一个录音机,附上录音机代码如下:

Java代码
  1. packagecom.sdvdxl.phonerecorder;
  2. importjava.io.File;
  3. importjava.io.IOException;
  4. importjava.text.SimpleDateFormat;
  5. importjava.util.Date;
  6. importandroid.media.MediaRecorder;
  7. importandroid.os.Environment;
  8. importandroid.util.Log;
  9. publicclassMyRecorder{
  10. privateStringphoneNumber;
  11. privateMediaRecordermrecorder;
  12. privatebooleanstarted=false;//录音机是否已经启动
  13. privatebooleanisCommingNumber=false;//是否是来电
  14. privateStringTAG="Recorder";
  15. publicMyRecorder(StringphoneNumber){
  16. this.setPhoneNumber(phoneNumber);
  17. }
  18. publicMyRecorder(){
  19. }
  20. publicvoidstart(){
  21. started=true;
  22. mrecorder=newMediaRecorder();
  23. FilerecordPath=newFile(
  24. Environment.getExternalStorageDirectory()
  25. ,"/Myrecord");
  26. if(!recordPath.exists()){
  27. recordPath.mkdirs();
  28. Log.d("recorder","创建目录");
  29. }
  30. StringcallDir="呼出";
  31. if(isCommingNumber){
  32. callDir="呼入";
  33. }
  34. StringfileName=callDir+"-"+phoneNumber+"-"
  35. +newSimpleDateFormat("yy-MM-dd_HH-mm-ss")
  36. .format(newDate(System.currentTimeMillis()))+".mp3";//实际是3gp
  37. FilerecordName=newFile(recordPath,fileName);
  38. try{
  39. recordName.createNewFile();
  40. Log.d("recorder","创建文件"+recordName.getName());
  41. }catch(IOExceptione){
  42. e.printStackTrace();
  43. }
  44. mrecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
  45. mrecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
  46. mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  47. mrecorder.setOutputFile(recordName.getAbsolutePath());
  48. try{
  49. mrecorder.prepare();
  50. }catch(IllegalStateExceptione){
  51. e.printStackTrace();
  52. }catch(IOExceptione){
  53. e.printStackTrace();
  54. }
  55. mrecorder.start();
  56. started=true;
  57. Log.d(TAG,"录音开始");
  58. }
  59. publicvoidstop(){
  60. try{
  61. if(mrecorder!=null){
  62. mrecorder.stop();
  63. mrecorder.release();
  64. mrecorder=null;
  65. }
  66. started=false;
  67. }catch(IllegalStateExceptione){
  68. e.printStackTrace();
  69. }
  70. Log.d(TAG,"录音结束");
  71. }
  72. publicvoidpause(){
  73. }
  74. publicStringgetPhoneNumber(){
  75. returnphoneNumber;
  76. }
  77. publicvoidsetPhoneNumber(StringphoneNumber){
  78. this.phoneNumber=phoneNumber;
  79. }
  80. publicbooleanisStarted(){
  81. returnstarted;
  82. }
  83. publicvoidsetStarted(booleanhasStarted){
  84. this.started=hasStarted;
  85. }
  86. publicbooleanisCommingNumber(){
  87. returnisCommingNumber;
  88. }
  89. publicvoidsetIsCommingNumber(booleanisCommingNumber){
  90. this.isCommingNumber=isCommingNumber;
  91. }
  92. }

到此,来去电通话自动录音的所有功能就完成了,大家可以自己试着编写并实现。

«上一页12下一页»


相关评论