Executing in OnCreate:
Copy Code code as follows:
public void OnCreate () {
Super.oncreate ();
LOG.I ("TAG", "service Started");
Monitor the caller status of the phone
Telephonymanager Telmanager = (Telephonymanager) This
. Getsystemservice (Context.telephony_service);
Register a listener to monitor the status of the phone
Telmanager.listen (New Myphonestatelistener (),
Phonestatelistener.listen_call_state);
}
Implement Myphonestatelistener:
Copy Code code as follows:
Private class Myphonestatelistener extends Phonestatelistener {
Mediarecorder Recorder;
File Audiofile;
String PhoneNumber;
public void oncallstatechanged (int state, String incomingnumber) {
Switch (state) {
Case TELEPHONYMANAGER.CALL_STATE_IDLE: * * without any State
if (recorder!= null) {
Recorder.stop (); Stop burning
Recorder.reset (); Re-set
Recorder.release (); Burn complete Make sure you release resources
}
Break
Case Telephonymanager.call_state_offhook: * * When the phone is picked up
try {
Recorder = new Mediarecorder ();
Recorder.setaudiosource (MediaRecorder.AudioSource.MIC); Set up audio capture original
Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Content output Format
Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); Audio encoding Mode
Recorder.setoutputfile ("/sdcard/myvoice.amr");
Audiofile = new File (
Environment.getexternalstoragedirectory (),
PhoneNumber + "_" + System.currenttimemillis ()
+ ". 3gp");
Recorder.setoutputfile (Audiofile.getabsolutepath ());
LOG.I ("TAG", Audiofile.getabsolutepath ());
Recorder.prepare (); Expected preparation
Recorder.start ();
catch (IllegalStateException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
Break
Case telephonymanager.call_state_ringing: * * When the phone came in
PhoneNumber = Incomingnumber;
Break
Default
Break
}
Super.oncallstatechanged (state, Incomingnumber);
}
}
Through the continuation of the two corresponding steps that can be implemented through the server to the telephone monitoring work, in Call_state_idle stateless (that is, idle state), Call_state_offhook phone call (that is, suspended), call_state_ringing The phone comes in (that is, when the call) executes in these several states.
Attached: Android photo, video, audio code example
Copy Code code as follows:
Package com.cons.dcg.collect;
Import Java.io.File;
Import Java.text.SimpleDateFormat;
Import java.util.*;
Import android.app.*;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.provider.MediaStore;
Import android.view.*;
Import android.widget.*;
public class Recordactivity extends activity implements Onclicklistener {
private static final int result_capture_image = 1;//Photography Requestcode
& nbsp; private static final int request_code_take_video = 2;//Camera Photography Requestcode
private static final int result_capture_recorder_sound = 3;// Recording Requestcode
private String Strimgpath = "";//Photo file absolute path
private String Strvideopath = "";//Video file absolute path
private String Strrecorderpath = "";//The absolute path of the recording file
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
This.setcontentview (R.layout.problem_report);
}
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
Switch (requestcode) {
Case result_capture_image://Photo shoot
if (ResultCode = = RESULT_OK) {
Toast.maketext (This, Strimgpath, Toast.length_short). Show ();
}
Break
Case request_code_take_video://Shooting Video
if (ResultCode = = RESULT_OK) {
Uri Urivideo = Data.getdata ();
Cursor cursor=this.getcontentresolver (). query (urivideo, NULL, NULL, NULL, NULL);
if (Cursor.movetonext ()) {
/** _data: Absolute path of file, _display_name: file name * *
Strvideopath = cursor.getstring (Cursor.getcolumnindex ("_data"));
Toast.maketext (This, Strvideopath, Toast.length_short). Show ();
}
}
Break
Case result_capture_recorder_sound://Recording
if (ResultCode = = RESULT_OK) {
Uri Urirecorder = Data.getdata ();
Cursor cursor=this.getcontentresolver (). query (urirecorder, NULL, NULL, NULL, NULL);
if (Cursor.movetonext ()) {
/** _data: Absolute path of file, _display_name: file name * *
Strrecorderpath = cursor.getstring (Cursor.getcolumnindex ("_data"));
Toast.maketext (This, Strrecorderpath, Toast.length_short). Show ();
}
}
Break
}
}
/**
* Photographic function
*/
private void Cameramethod () {
Intent imagecaptureintent = new Intent (mediastore.action_image_capture);
Strimgpath = Environment.getexternalstoragedirectory (). toString () + "/consdcgmpic/";//folder where photos are stored
String fileName = new SimpleDateFormat ("YYYYMMDDHHMMSS"). Format (new Date ()) + ". jpg";//Photo naming
File out = new file (Strimgpath);
if (!out.exists ()) {
Out.mkdirs ();
}
out = new File (Strimgpath, fileName);
Strimgpath = strimgpath + filename;//absolute path to this photo
Uri uri = Uri.fromfile (out);
Imagecaptureintent.putextra (Mediastore.extra_output, URI);
Imagecaptureintent.putextra (mediastore.extra_video_quality, 1);
Startactivityforresult (Imagecaptureintent, result_capture_image);
}
/**
* Shooting video
*/
private void Videomethod () {
Intent Intent = new Intent (mediastore.action_video_capture);
Intent.putextra ( mediastore.extra_video_quality, 0);
Startactivityforresult (Intent, request_code_take_video);
}
/**
* Recording function
*/
private void Soundrecordermethod () {
Intent Intent = new Intent (intent.action_get_content);
Intent.settype (" Audio/amr ");
Startactivityforresult (Intent, result_capture_recorder_sound);
}
/**
* Prompt Information
* @param text
* @param duration
*/
private void Showtoast (String text, int duration) {
Toast.maketext (Problemreport.this, text, duration). Show ();
}
}