Android call recording implementation

Source: Internet
Author: User

Due to System Restrictions, only the voice of the microphone can be recorded, and the voice of the other speaker cannot be recorded. You may need to change the kernel to implement two-way recording;

When the phone is connected and the phone is hung up, shake it down;

Use broadcast receiver to enable auto-start;

 

Service Code:

 

Package com. eboy. phonelistener;

Import java. Io. file;

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. OS. vibrator;
Import Android. telephony. phonestatelistener;
Import Android. telephony. telephonymanager;
Import Android. widget. Toast;

Public class phoneservice extends Service {

@ Override
Public ibinder onbind (intent arg0 ){
// Todo auto-generated method stub
Return NULL;
}

@ Override
Public void oncreate (){
// Todo auto-generated method stub
Super. oncreate ();
Telephonymanager = (telephonymanager) getsystemservice (context. telephony_service );
Telephonymanager. Listen (New phonelistener (), phonestatelistener. listen_call_state );
}

Private final class phonelistener extends phonestatelistener {
Private mediarecorder;
Private file;
Private vibrator = (vibrator) getsystemservice (vibrator_service );

@ Override
Public void oncallstatechanged (INT state, string incomingnumber ){
Try {

Switch (state ){
Case telephonymanager. call_state_ringing: // incoming call
Break;

Case telephonymanager. call_state_offhook: // call
File = new file (environment. getexternalstoragedirectory (), incomingnumber + system. currenttimemillis () + ". 3GP ");
Mediarecorder = new mediarecorder ();
Mediarecorder. setaudiosource (mediarecorder. audiosource. Mic );
Mediarecorder. setoutputformat (mediarecorder. outputformat. three_gpp );
Mediarecorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );
Mediarecorder. setoutputfile (file. getabsolutepath ());
Mediarecorder. Prepare ();
Mediarecorder. Start ();
Toast. maketext (getapplicationcontext (), "The phone is connected and the recording starts. ", Toast. length_short). Show ();
// Vibrate
Vibrator. vibrate (100 );
Break;

Case telephonymanager. call_state_idle: // call up
If (mediarecorder! = NULL ){
Mediarecorder. Stop ();
Mediarecorder. Release ();
Mediarecorder = NULL;
Toast. maketext (getapplicationcontext (), "The phone has been hung up and the recording has stopped. ", Toast. length_short). Show ();
// Vibrate
Vibrator. vibrate (100 );
}
Break;
}

} Catch (exception e ){
E. printstacktrace ();
}
}
}
}

 

 

The broadcast receiver is used for self-starting. The Code is as follows:

Package com. eboy. phonelistener;

Import Android. content. context;
Import Android. content. intent;

Public class broadcastreceiver extends Android. content. broadcastreceiver {

@ Override
Public void onreceive (context arg0, intent arg1 ){
Intent service = new intent (arg0, phoneservice. Class );
Arg0.startservice (service );
}
}

 

Main. xml:

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. eboy. phonelistener"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">

<Uses-SDK Android: minsdkversion = "7" Android: targetsdkversion = "15"/>

<Application Android: Label = "@ string/app_name"
Android: icon = "@ drawable/ic_launcher"
Android: theme = "@ style/apptheme">

<Service android: Name = ". phoneservice"> </service>
<Cycler Android: Name = ". broadcastreceiver">
<Intent-filter>

<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>

</Application>

<! -- Read the phone status permission -->
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
<! -- SD card read and write permissions -->
<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
<! -- Recording permission -->
<Uses-Permission Android: Name = "android. Permission. record_audio"/>
<! -- Shake permission -->
<Uses-Permission Android: Name = "android. Permission. Vibrate"/>

</Manifest>

 

 

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.