标签:
Registers a listener object to receive notification of changes
in specified telephony states. |
//1、通过服务获得TelePhonemanger
TelePhonemanger tele = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//2、注册一个电话的监听listener 监听电话状态
telephonyManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
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);
}
}
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"/>
TelephonyManager tele = (TelephonyManager) getSystemService(TELEPHONY_SERVICE
int state = tele.getCallState();
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. |
标签:
原文地址:http://www.cnblogs.com/candledragle/p/4245039.html