Configuration File
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "codefans. APP "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <! -- Listen to the phone status permission --> <uses-Permission Android: Name = "android. Permission. read_phone_state"/> <! -- Create and delete file permissions in sdcard --> <uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/> <! -- Write data permission on sdcard --> <uses-Permission Android: Name = "android. Permission. write_external_storage"/> <! -- Enter the voice permission --> <uses-Permission Android: Name = "android. permission. record_audio "/> <application Android: icon =" @ drawable/ic_launcher "Android: Label =" @ string/app_name "> <service android: Name = ". phoneservice "> </service> <receiver Android: Name = ". phonebroadcastreceiver "> <intent-filter> <action Android: Name =" android. intent. action. boot_completed "/> <! -- Start broadcast at startup --> </intent-filter> </receiver> </Application> </manifest>
Public class phoneservice extends Service {@ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL;} @ overridepublic void oncreate () {super. oncreate (); telephonymanager = (telephonymanager) getsystemservice (context. telephony_service); telephonymanager. listen (New phonelistener (), phonestatelistener. listen_call_state); // registers the listener to listen to the phone status} private final class Phonelistener extends phonestatelistener {private string incomenumber; // call number private mediarecorder; private file; @ overridepublic void oncallstatechanged (INT state, string incomingnumber) {try {Switch (state) {Case telephonymanager. call_state_ringing: // call this. incomenumber = incomingnumber; break; Case telephonymanager. call_state_offhook: // call file = new file (environment. getexternalsto Ragedirectory (), this. incomenumber + system. currenttimemillis () + ". 3GP "); mediarecorder = new mediarecorder (); mediarecorder. setaudiosource (mediarecorder. audiosource. MIC); // obtain the audio data source mediarecorder. setoutputformat (mediarecorder. outputformat. three_gpp); // output mediarecorder in 3GP format. setaudioencoder (mediarecorder. audioencoder. amr_nb); mediarecorder. setoutputfile (file. getabsolutepath (); // output file mediarecorder. P Repare (); // prepare mediarecorder. Start (); break; Case telephonymanager. call_state_idle: // disconnect the call if (mediarecorder! = NULL) {mediarecorder. stop (); mediarecorder. release (); mediarecorder = NULL;} break;} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}
Public class phonebroadcastreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {intent service = new intent (context, phoneservice. class); context. startservice (service); // start the service }}