Monitor phone recording

Source: Internet
Author: User

Requirements Analysis:Record the recording during the call and save to the specified path ~
Knowledge Points: 1. The mobile phone starts broadcasting, the broadcast recipient receives the notification, opens the service service 2.Service work flow:
1. Get the phone management object ·
2. Register the Listener, corresponding to the phone status
3. According to the phone status (ringing, calling, spare), create Mediaplay object, pause here to monitor the status of the phone, join? permissions <uses-permission android:name= "Android.permission.READ_ Phone_state "/> Code:Telephonymanager manager= (Telephonymanager) Getsystemservice (telephony_service);//Get Phone managementManager.listen (New Myphonestatelistener (), phonestatelistener.listen_call_state);//Use the Phone Manager to register a listener to monitor phone statusNote: The phone status monitoring this, can listen to multiple states,listen_call_state Monitoring is the change in the status of the phone, the following listener other listenersState three values for phone status change: Spare, call, hang up
According to three states, processing MediaPlayer, code

Public void oncallstatechanged (int state, String incomingnumber) {//Phone state changes when you run the methodswitch (state) { case telephonymanager.call_state_ringing:log.d ("Debug", "ringing"); num = incomingnumber; / /When ringing, record number break; case Telephonymanager.call_state_offhook:log.d ("Debug", "Start recording"); Startrecording (); / //Pick-up, start recording break; case Telephonymanager.call_state_idle:log.d ("Debug", "spare status"); Stoprecording (); / /Spare, end recording break;}}think: Suppose the phone doesn't ring? Stop recording:private void stoprecording () {if (Mrecorder! = null) {Mrecorder.stop ();Stop it Mrecorder.release (); Freeing resources Mrecorder = null;Garbage collection}} Start Recording:private void startrecording () {try {LOG.D ("Debug", "startrecording");Mrecorder = new Mediarecorder (); Create Media recorderMrecorder.setaudiosource (MediaRecorder.AudioSource.MIC);Set up an audio sourceMrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Set Output formatMrecorder.setoutputfile ("/mnt/sdcard/" + num + "_" + system.currenttimemillis () + ". 3gp");                             Set Output file pathMrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB);//Set encodingMrecorder.prepare ();Get readyMrecorder.start ();Start} catch (Exception e) {E.printstacktrace ();} }}} Summary: MediaPlayer operation process, stop and recording process ~, here used to sdcard so, you must declare permissions. <uses-permission android:name= "Android.permission.RECORD_AUDIO"/>//Consent Program for recording <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>//write memory cardconfiguration information for the manifest file:<receiver android:name= ". Phonebroacast "><intent-filter ><action android:name= "Android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver><service android:name= ". Phoneservice "></service>

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.