电话状态、电话录音、取消电话监听
获得电话状态需要在请电脑文件中添加如下权限
监听电话的步骤:
说用到主要方法:
Registers a listener object to receive notification of changes
in specified telephony states. |
//1、通过服务获得TelePhonemanger
TelePhonemanger telephoneManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//2、注册一个电话的监听listener 监听电话状态
telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
写一个内部类MyPhoneStateListener 继承与 PhoneStateListener
private class MyListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(state==tele.CALL_STATE_RINGING){//来电状态
System.out.println("弹出土司------------电话号码归属地");
}else if(state==tele.CALL_STATE_OFFHOOK){//接通状态
System.out.println("土司关闭");
}
super.onCallStateChanged(state, incomingNumber);
}
}
相关实例:
1、电话录音:
public class PhoneService extends Service {
private TelephonyManager telephonyManager;
//定义一个 录音机
private MediaRecorder recorder;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
// 在 把 TelephonyManager 给初始化
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
/**
* 注册一个电话的监听
* listener 监听电话状态
*/
telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
super.onCreate();
}
private class MyPhoneStateListener extends PhoneStateListener{
//当电话状态发生改变的时候调用
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: // 挂断 或者 未接通
System.out.println("判断一下录音机是否开启 如果开启 停止录音");
if (recorder!=null) {
recorder.stop(); //停止录
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接通状态
System.out.println("开始录音");
//开始录
recorder.start(); // Recording is now started
break;
case TelephonyManager.CALL_STATE_RINGING: //响铃状态
System.out.println("准备一个录音机出来");
//初始化 录音机
recorder = new MediaRecorder();
// 设置音频来源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // zet 华为
// 设置音频输出的格式 3gp
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置音频编码方式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置音频保存的路径
recorder.setOutputFile("/mnt/sdcard/haha.3gp");
//开始准备录
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
注:在清单中需要添加的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
常用到的电话状态:
1、通过系统服务拿到TelephoneManager
TelephonyManager tele = (TelephonyManager) getSystemService(TELEPHONY_SERVICE
2、获得电话的几种常用状态
3、获得当前电话状态:
int state = tele.getCallState();
如何取消电话监听服务:
telePhoneManager.listen(listener,PhoneStateListener.LISTEN_NINE);
listener=null;
常用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. |