Today I learned about inter-process service communication-> aidl. Based on the broadcast mechanism I learned a few days ago, I made a simple phone eavesdropping and recording application. The specific implementation method is attached below for your reference. I hope you can provide some valuable comments.
Business Requirement Analysis:
1. When the mobile phone is turned on, the listening service will start and listen for incoming calls.
2. Set the phone blacklist. When a call is blacklisted, it will be hung up directly.
Steps:
First, we need to define a telephone monitoring service to listen for and intercept incoming calls. The Code is as follows:
Phonelistenerservice:
package cn.yj3g.L21_PhoneListener;import java.lang.reflect.Method;import com.android.internal.telephony.ITelephony;import android.app.Service;import android.content.Context;import android.content.Intent;import android.media.MediaRecorder;import android.os.Environment;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.Toast;public class PhoneListenerService extends Service { private MediaRecorder recorder; private boolean recording = false; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Log.v("TAG", "service onCreate()"); super.onCreate(); //电话服务管理 TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); //监听电话状态 manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } private PhoneStateListener listener = new PhoneStateListener() { /* * @see TelephonyManager#CALL_STATE_IDLE 值为0 * * @see TelephonyManager#CALL_STATE_RINGING 值为1 * * @see TelephonyManager#CALL_STATE_OFFHOOK 值为2 */ @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); //打印电话状态改变信息 Log.v("TAG", "onCallStateChanged state=" + state); switch (state) { case TelephonyManager.CALL_STATE_IDLE: // 没有来电 或者 挂断 stopRecord(); break; case TelephonyManager.CALL_STATE_RINGING: // 响铃时 stop(incomingNumber); break; case TelephonyManager.CALL_STATE_OFFHOOK: // 接起电话 recordCalling(); break; default: break; } } }; //停止录音 private void stopRecord() { Log.v("TAG", "stopRecord"); if (recording) { recorder.stop(); recorder.release(); recording=false; } } //电话拦截 public void stop(String s) { try { if (s.equals("110")) { Toast.makeText(this, "拦截成功", 0).show(); Log.e("TAG", "此来电为黑名单号码,已被拦截!"); //调用ITelephony.endCall()结束通话 Method method = Class.forName("android.os.ServiceManager") .getMethod("getService", String.class); IBinder binder = (IBinder) method.invoke(null, new Object[] { TELEPHONY_SERVICE }); ITelephony telephony = ITelephony.Stub.asInterface(binder); telephony.endCall(); } else Toast.makeText(this, "不需拦截", 0).show(); recording=false; } catch (Exception e) { e.printStackTrace(); } } //进行录音 private void recordCalling() { try { Log.v("TAG", "recordCalling"); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 读麦克风的声音 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 输出格式.3gp recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 编码方式 recorder.setOutputFile(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/" + System.currentTimeMillis() + ".3gp");// 存放的位置是放在sdcard目录下 recorder.prepare(); recorder.start(); recording = true; } catch (Exception e) { e.printStackTrace(); } }}Android does not open an API for ending a call. to end a call, you must use aidl to communicate with the phone Management Service and call the API in the service to end the call. The method is as follows: 1. copy the following files from the android source code to the project: COM/Android/Internal/telephony/itelephony. aidlandroid/telephony/neighboringcellinfo. shows aidl. the development tool automatically generates itelephony In the gen directory. java
We know that the service cannot be started by ourselves and must be started manually. Therefore, we need a broadcast. When the mobile phone is started, we send a broadcast to start the listening service. The following is a broadcaster I wrote to send broadcasts.
package cn.yj3g.L21_PhoneListener;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //运用广播开启监听这个服务 Log.v("TAG", "开机了!"); Intent i = new Intent(context, PhoneListenerService.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//由于之前运行不能实现功能,经老师的修改加上一个任务标志 context.startService(i); }}
The following code configures related permissions in androidmanifest. xml:
Androidmanifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.yj3g.L21_PhoneListener" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <service android:name=".PhoneListenerService"> <intent-filter> <action android:name="cn.yj3g.L21_PhoneListener.PhoneListenerService" ></action> </intent-filter> </service> <receiver android:name=".BootCompleteReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> <!-- 读取电话状态权限--> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <!-- 录音权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <!-- 向sdcard中写数据的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 开机启动广播的权限 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- 挂断电话时需要的权限 --> <uses-permission android:name="android.permission.CALL_PHONE" /></manifest>
This application is ready to listen to the phone. When the mobile phone that installs this application is turned on, the incoming call is in the listening status, so that it can listen to or intercept the incoming call of the mobile phone with a sound color, in order to achieve the ulterior motives.
Phone eavesdropping and interception applications