标签:
使用BordercastReceiver和Service组件实现下述功能:
1.当手机处于来电状态,启动监听服务,对来电进行监听录音。
2.设置电话黑名单,当来电是黑名单电话,则直接挂断。
当拨打电话或电话状态发生改变时,系统就会发出有序广播,因此我们可以使用BordercastReceiver接受广播,因BordercastReceiver执行时间短不能执行耗时任务也不能使用子线程,因此我们应启动一个Service来监听电话并进行处理
二、加入AIDL文件
Android没有对外公开结束通话的API,要结束通话就必须使用AIDL与电话管理服务进行通信,并调用服务中的API实现结束通话,这样需要android 源码文件NeighboringCellInfo.aidl和ITelephony.aidl添加到项目中,如图所示:
Android Studio 会自动编译产生对应的类文件
三、编写TelReceiver组件
- public class TelReceiver extends BroadcastReceiver {
- public TelReceiver() {
- }
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Intent i=new Intent(context,ListenPhoneService.class);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.setAction(intent.getAction());
- i.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER,
- intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
- i.putExtra(TelephonyManager.EXTRA_STATE,
- intent.getStringExtra(TelephonyManager.EXTRA_STATE));
- context.startService(i);
- }
- }
注册广播:
- <receiver android:name=".TelReceiver" >
- <intent-filter android:priority="1000" >
- <action android:name="android.intent.action.PHONE_STATE" />
- <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
- </intent-filter>
- </receiver>
四、编写ListenPhoneService组件
- public class ListenPhoneService extends Service {
- private AudioManager mAudioManager;
- private TelephonyManager tm;
- public ListenPhoneService() {
- }
-
- @Override
- public void onCreate() {
- super.onCreate();
- mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
- tm=(TelephonyManager)getSystemService(Service.TELEPHONY_SERVICE);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
- }else{
- tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
- }
-
- return super.onStartCommand(intent, flags, startId);
- }
-
- private void stopCall(){
- try {
- Method method=Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
- IBinder binder=(IBinder)method.invoke(null, new Object[]{"phone"});
- ITelephony telephoney=ITelephony.Stub.asInterface(binder);
- telephoney.endCall();
- stopSelf();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- PhoneStateListener listener=new PhoneStateListener(){
- @Override
- public void onCallStateChanged(int state,String incomingNumber){
- switch (state){
- case TelephonyManager.CALL_STATE_IDLE:
- stopCall();
- break;
- case TelephonyManager.CALL_STATE_OFFHOOK:
- recordCall();
- break;
- case TelephonyManager.CALL_STATE_RINGING:
- Log.e("jereh", "来电号码是:"+ incomingNumber);
- if (incomingNumber.equals("123456")) {
- stopCall();
- }
- break;
- }
- }
- };
-
- private void stopRecord(){
- if(recording){
- recorder.stop();
- recorder.release();
- recording=false;
- stopSelf();
- }
- }
- private MediaRecorder recorder;
- private boolean recording ;
- private void recordCall(){
- Log.d("jereh", "record calling")
- if( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- recorder=new MediaRecorder();
- recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
- recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- File file=new File(Environment.getDownloadCacheDirectory().getAbsolutePath(),"recorder");
- if(!file.exists()){
- file.mkdir();
- }
- recorder.setOutputFile(file.getAbsolutePath() + "/"
- + System.currentTimeMillis() + "3gp");
- try {
- recorder.prepare();
- recorder.start();
- recording=true;
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- throw new UnsupportedOperationException("Not yet implemented");
- }
- }
-
- 复制代码
-
- Service XML配置
-
- <service
- android:name=".ListenPhoneService"
- android:enabled="true"
- android:exported="true" >
- </service>
五、最后别忘了一些权限的设置
- <!-- 添加访问手机电话状态的权限 -->
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <!-- 拨打电话权限 -->
- <uses-permission android:name="android.permission.CALL_PHONE" />
- <!-- 监听手机去电的权限 -->
- <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
- <!-- 在SDCard中创建与删除文件权限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <!-- 往SDCard写入数据权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
全能程序员交流QQ群290551701,群内程序员都是来自,百度、阿里、京东、小米、去哪儿、饿了吗、蓝港等高级程序员 ,拥有丰富的经验。加入我们,直线沟通技术大牛,最佳的学习环境,了解业内的一手的资讯。如果你想结实大牛,那 就加入进来,让大牛带你超神!
Android四大组件应用系列——实现电话拦截和电话录音
标签:
原文地址:http://www.cnblogs.com/fengliucaizi/p/4900405.html