首页 > 代码库 > 电话状态、电话录音

电话状态、电话录音

获得电话状态需要在请电脑文件中添加如下权限

监听电话的步骤:
    说用到主要方法:
void listen(PhoneStateListener listener, int events)
Registers a listener object to receive notification of changes in specified telephony states.
  1. //1、通过服务获得TelePhonemanger
  2. TelePhonemanger tele = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  3. //2、注册一个电话的监听listener 监听电话状态
  4. telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
写一个内部类MyPhoneStateListener 继承与 PhoneStateListener
  1. private class MyListener extends PhoneStateListener{
  2. @Override
  3. public void onCallStateChanged(int state, String incomingNumber) {
  4. if(state==tele.CALL_STATE_RINGING){//来电状态
  5. System.out.println("弹出土司------------电话号码归属地");
  6. }else if(state==tele.CALL_STATE_OFFHOOK){//接通状态
  7. System.out.println("土司关闭");
  8. }
  9. super.onCallStateChanged(state, incomingNumber);
  10. }
  11. }

相关实例:
1、电话录音:
  1. public class PhoneService extends Service {
  2. private TelephonyManager telephonyManager;
  3. //定义一个 录音机
  4. private MediaRecorder recorder;
  5. @Override
  6. public IBinder onBind(Intent intent) {
  7. return null;
  8. }
  9. @Override
  10. public void onCreate() {
  11. // 在 把 TelephonyManager 给初始化
  12. telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  13. /**
  14. * 注册一个电话的监听
  15. * listener 监听电话状态
  16. */
  17. telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
  18. super.onCreate();
  19. }
  20. private class MyPhoneStateListener extends PhoneStateListener{
  21. //当电话状态发生改变的时候调用
  22. @Override
  23. public void onCallStateChanged(int state, String incomingNumber) {
  24. switch (state) {
  25. case TelephonyManager.CALL_STATE_IDLE: // 挂断 或者 未接通
  26. System.out.println("判断一下录音机是否开启 如果开启 停止录音");
  27. if (recorder!=null) {
  28. recorder.stop(); //停止录
  29. recorder.reset(); // You can reuse the object by going back to setAudioSource() step
  30. recorder.release(); // Now the object cannot be reused
  31. }
  32. break;
  33. case TelephonyManager.CALL_STATE_OFFHOOK: //接通状态
  34. System.out.println("开始录音");
  35. //开始录
  36. recorder.start(); // Recording is now started
  37. break;
  38. case TelephonyManager.CALL_STATE_RINGING: //响铃状态
  39. System.out.println("准备一个录音机出来");
  40. //初始化 录音机
  41. recorder = new MediaRecorder();
  42. // 设置音频来源
  43. recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // zet 华为
  44. // 设置音频输出的格式 3gp
  45. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  46. // 设置音频编码方式
  47. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  48. //设置音频保存的路径
  49. recorder.setOutputFile("/mnt/sdcard/haha.3gp");
  50. //开始准备录
  51. try {
  52. recorder.prepare();
  53. } catch (IllegalStateException e) {
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. break;
  59. default:
  60. break;
  61. }
  62. super.onCallStateChanged(state, incomingNumber);
  63. }
  64. }
  65. }
注:在清单中需要添加的权限:
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  2. <uses-permission android:name="android.permission.RECORD_AUDIO" />
  3. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>



常用到的电话状态:
1、通过系统服务拿到TelephoneManager
  1. TelephonyManager tele = (TelephonyManager) getSystemService(TELEPHONY_SERVICE
2、获得电话的几种常用状态
CALL_STATE_IDLE:表示电话处于闲置状态
CALL_STATE_OFFHOOK:表示电话处于接通状态
CALL_STATE_RINGING:表示电话处于响铃状态
3、获得当前电话状态:
  1. int state = tele.getCallState();




常用api预览:
String ACTION_PHONE_STATE_CHANGED Broadcast intent action indicating that the call state (cellular) on the device has changed.
int CALL_STATE_IDLE Device call state: No activity.
int CALL_STATE_OFFHOOK Device call state: Off-hook.
int CALL_STATE_RINGING Device call state: Ringing.
int DATA_ACTIVITY_DORMANT Data connection is active, but physical link is down
int DATA_ACTIVITY_IN Data connection activity: Currently receiving IP PPP traffic.
int DATA_ACTIVITY_INOUT Data connection activity: Currently both sending and receiving IP PPP traffic.
int DATA_ACTIVITY_NONE Data connection activity: No traffic.
int DATA_ACTIVITY_OUT Data connection activity: Currently sending IP PPP traffic.
int DATA_CONNECTED Data connection state: Connected.
int DATA_CONNECTING Data connection state: Currently setting up a data connection.
int DATA_DISCONNECTED Data connection state: Disconnected.
int DATA_SUSPENDED Data connection state: Suspended.


来自为知笔记(Wiz)


电话状态、电话录音