android实现电话录音

配置文件

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="codefans.app"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk android:minSdkVersion="8" />
  7. <!-- 监听电话状态权限 -->
  8. <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  9. <!-- 在SDCard中创建与删除文件权限 -->
  10. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
  11. <!-- 在SDCard写入数据权限 -->
  12. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  13. <!-- 刻入声音权限 -->
  14. <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  15. <application
  16. android:icon="@drawable/ic_launcher"
  17. android:label="@string/app_name" >
  18. <service android:name=".PhoneService"></service>
  19. <receiver android:name=".PhoneBroadcastReceiver">
  20. <intent-filter >
  21. <action android:name="android.intent.action.BOOT_COMPLETED"/> <!-- 开机启动广播 -->
  22. </intent-filter>
  23. </receiver>
  24. </application>
  25. </manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="codefans.app" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <!-- 监听电话状态权限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 在SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 刻入声音权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <service android:name=".PhoneService"></service> <receiver android:name=".PhoneBroadcastReceiver"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> <!-- 开机启动广播 --> </intent-filter> </receiver> </application> </manifest>
[java] view plain copy print ?
  1. public class PhoneService extends Service
  2. {
  3. @Override
  4. public IBinder onBind(Intent intent)
  5. {
  6. // TODO Auto-generated method stub
  7. return null;
  8. }
  9. @Override
  10. public void onCreate()
  11. {
  12. super.onCreate();
  13. TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  14. telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE); //注册监听器 监听电话状态
  15. }
  16. private final class PhoneListener extends PhoneStateListener
  17. {
  18. private String incomeNumber; //来电号码
  19. private MediaRecorder mediaRecorder;
  20. private File file;
  21. @Override
  22. public void onCallStateChanged(int state, String incomingNumber)
  23. {
  24. try
  25. {
  26. switch(state)
  27. {
  28. case TelephonyManager.CALL_STATE_RINGING: //来电
  29. this.incomeNumber = incomingNumber;
  30. break;
  31. case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话
  32. file = new File(Environment.getExternalStorageDirectory(), this.incomeNumber + System.currentTimeMillis() + ".3gp");
  33. mediaRecorder = new MediaRecorder();
  34. mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); //获得声音数据源
  35. mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 按3gp格式输出
  36. mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  37. mediaRecorder.setOutputFile(file.getAbsolutePath()); //输出文件
  38. mediaRecorder.prepare(); //准备
  39. mediaRecorder.start();
  40. break;
  41. case TelephonyManager.CALL_STATE_IDLE: //挂掉电话
  42. if(mediaRecorder != null)
  43. {
  44. mediaRecorder.stop();
  45. mediaRecorder.release();
  46. mediaRecorder = null;
  47. }
  48. break;
  49. }
  50. }
  51. catch (IllegalStateException e)
  52. {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. catch (IOException e)
  57. {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63. }
public class PhoneService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE); //注册监听器 监听电话状态 } private final class PhoneListener extends PhoneStateListener { private String incomeNumber; //来电号码 private MediaRecorder mediaRecorder; private File file; @Override public void onCallStateChanged(int state, String incomingNumber) { try { switch(state) { case TelephonyManager.CALL_STATE_RINGING: //来电 this.incomeNumber = incomingNumber; break; case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话 file = new File(Environment.getExternalStorageDirectory(), this.incomeNumber + System.currentTimeMillis() + ".3gp"); mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //获得声音数据源 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 按3gp格式输出 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(file.getAbsolutePath()); //输出文件 mediaRecorder.prepare(); //准备 mediaRecorder.start(); break; case TelephonyManager.CALL_STATE_IDLE: //挂掉电话 if(mediaRecorder != null) { mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder = null; } break; } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
[java] view plain copy print ?
  1. public class PhoneBroadcastReceiver extends BroadcastReceiver
  2. {
  3. @Override
  4. public void onReceive(Context context, Intent intent)
  5. {
  6. Intent service = new Intent(context, PhoneService.class);
  7. context.startService(service); //启动服务
  8. }
  9. }
public class PhoneBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, PhoneService.class); context.startService(service); //启动服务 } }

你可能感兴趣的:(android,监听,电话录音)