标签:
在清单文件中添加如下配置
<!-- 配置广播接收者 -->
<receiver android:name="com.istarry.ipbohao.IpCallReceiver" >
<intent-filter>
<!-- 指定接收那个广播 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<!-- <data android:scheme=" " /> -->
</intent-filter>
</receiver>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter("android.intent.action.MEDIA_UNMOUNTED");
intentFilter.addDataScheme("file");
registerReceiver(new IpCallReceiver(), intentFilter);
}
}
需求:
1.拦截广播
<!-- 指定接收(拦截)那个广播 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
2.需要的权限
<uses-permission android:name="android.permission.PROCESSOUTGOINGCALLS" />
3.创建一个类IpCallReceiver继承BroadCastReceiver
public class IpCallReceiver extends BroadcastReceiver {
//当收到广播时,此方法调用
@Override
public void onReceive(Context arg0, Intent arg1) {
//添加17951线路
//1.拿到用户拨打的号码
//广播会发送一个Intent,携带数据
String number = getResultData();
//在号码前加上17951,并返回数据
setResultData("17951"+number);
}
}
4.在清单文件中注册广播接收者
<application
…… >
…………
<!-- 配置广播接收者 -->
<receiver android:name="com.istarry.ipbohao.IpCallReceiver" >
<!-- android:priority="1000"代表着给当前接收者设置优先级,优先级越高就越先接收到广播,取值-1000~1000 -->
<intent-filter android:priority="1000">
<!-- 指定接收那个广播 -->
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<!-- <data android:scheme=" " /> -->
</intent-filter>
</receiver>
…………
进程优先级由高到低,依次为:
Android
TelephonyManager,设置PhoneStateListener监听状态
权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />
配置清单文件
<service android:name="com.istarry.luyinji.LuyinjiService" />
创建一个类LuyinjiService继承Service
public class LuyinjiService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // 获取电话管理器 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 设置侦听 // 参数2:设置监听器只监听什么数据 tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE); } private MediaRecorder recorder; class MyListener extends PhoneStateListener { // 电话状态改变时调用此方法 @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: System.out.println("空闲");
if (recorder != null) {
// 停止录音
recorder.stop();
// 释放占用的资源
recorder.release();
recorder = null;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
System.out.println("响铃");
if (recorder == null) {
recorder = new MediaRecorder();
// 设置录音源
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置录音格式3gp格式
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置录音文件保存位置
recorder.setOutputFile("sdcard/voice.3gp");
// 设置音频编码(一种格式可能有多重种编码方式)
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
// 准备完毕,随时可以录音
recorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("摘机");
if (recorder != null) {
recorder.start();
}
break;
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
}
标签:
原文地址:http://www.cnblogs.com/istarry/p/4440082.html