I. Description of the problem
Use the Bordercastreceiver and service components to implement the following features:
1. When the phone is in the state of the call, start the listening service, the caller to monitor the recording.
2. Set up the telephone blacklist, when the call is the blacklist phone, then hang up directly.
When the status of the call or phone changes, the system emits an orderly broadcast, so we can use Bordercastreceiver to accept the broadcast, because the Bordercastreceiver execution time is too short to perform time-consuming tasks and can not use child threads, So we should start a service to listen and process the phone.
Ii. inclusion of AIDL documents
Android does not have an open end call API, and to end the call you must use Aidl to communicate with the Telephony Management Service and call the API in the service to end the call, which requires Android Source files Neighboringcellinfo.aidl and Itelephony.aidl are added to the project as shown in the figure:
Android Studio automatically compiles and generates the corresponding class file
Iii. Writing Telreceiver Components
public class Telreceiver extends Broadcastreceiver {public
telreceiver () {
}
@Override public
Void OnReceive (context context, Intent Intent) {
Intent i=new Intent (context,listenphoneservice.class);
I.setflags (intent.flag_activity_new_task);
I.setaction (Intent.getaction ());
I.putextra (Telephonymanager.extra_incoming_number,
Intent.getstringextra (telephonymanager.extra_incoming_ Number))//Telephone
I.putextra (telephonymanager.extra_state,
Intent.getstringextra ( telephonymanager.extra_state))//Phone status
context.startservice (i);//Start Service
}
}
Register for Broadcast:
<receiver android:name= ". Telreceiver ">
<intent-filter android:priority=" 1000 ">
<action android:name=" Android.intent.action.PHONE_STATE "/>
<action android:name=" Android.intent.action.NEW_OUTGOING_CALL " >
</intent-filter>
</receiver>
Four, writing Listenphoneservice components
public class Listenphoneservice extends Service {private Audiomanager maudiomanager;
Private Telephonymanager TM;
Public Listenphoneservice () {} @Override public void OnCreate () {super.oncreate ();
Maudiomanager= (Audiomanager) Getsystemservice (Context.audio_service);
Tm= (Telephonymanager) Getsystemservice (Service.telephony_service); @Override public int Onstartcommand (Intent Intent, int flags, int startid) {if (intent.getaction). Equals (Intent.acti On_new_outgoing_call) {//Go to the radio, Android did not come to the telephone broadcast}else{//remove the call is the status of the caller//Method 1//Get the call//String number=
Intent.getstringextra (Telephonymanager.extra_incoming_number);
Get phone status//String State=intent.getstringextra (telephonymanager.extra_state);
LOG.D ("Jereh", "Incoming phone:" + number);
LOG.D ("Jereh", "Call State:" +state); Telephonymanager.extra_state_idle: No incoming call or hang up//Telephonymanagerextra_state_offhook: Pick up the phone///Telephonymanager.extra _state_ringing: When the call is called, the bell//if (state.equals) (Telephonymanager.extra_state_ringiNG)) {//if (Number.equals ("13280998858")) {//intercept the specified phone number//Maudiomanager.setringermode (Audiomanager.ringer_mode_
Silent);
LOG.D ("Jereh", "telephone interception");
Stopcall (); Maudiomanager.setringermode (audiomanager.ringer_mode_normal);//recovery ringtones//}//}else if (State.equals ( Telephonymanager.extra_state_offhook)) {//Recordcall ()//Start recording//}else if (State.equals ( Telephonymanager.extra_state_idle)) {//Stopcall ()//Stop recording//}//Method 2//Set up a listener, monitor phone status Tm.listen (listener,phonestat
Elistener.listen_call_state);
Return Super.onstartcommand (Intent, flags, Startid);
/** * Hang up phone/private void Stopcall () {try {//android's design will be servicemanager hidden, so it can only be obtained using reflection mechanism.
Method Method=class.forname ("Android.os.ServiceManager"). GetMethod ("GetService", String.class); IBinder binder= (ibinder) Method.invoke (null, new object[]{"Phone"})//Get System Telephony service Itelephony telephoney=
ITelephony.Stub.asInterface (binder); Telephoney.endcall ()//Hang up the phone stopself ()//Stop Service} catch (Exception e) {E.printStackTrace (); } phonestatelistener listener=new Phonestatelistener () {@Override public void oncallstatechanged (int state,string in
Comingnumber) {switch (state) {//Mobile phone idle case TelephonyManager.CALL_STATE_IDLE:stopCall ();/stop recording break;
Pick up the phone case TelephonyManager.CALL_STATE_OFFHOOK:recordCall ()//start recording break;
When the case telephonymanager.call_state_ringing:log.e ("Jereh", "Caller number is:" + incomingnumber);
If the number belongs to the blacklist if (Incomingnumber.equals ("123456")) {//If it is a blacklist, shield Stopcall ();
} break;
}
}
};
/** * Stop Recording * * private void Stoprecord () {if (recording) {recorder.stop ();
Recorder.release ();
Recording=false;
Stopself ()//Stop Service}/** * Telephone recording/private Mediarecorder recorder;
Private Boolean recording; private void Recordcall () {log.d ("Jereh", "record Calling") if (Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {recorder=new mediarecorder ()); Recorder.setaudiosource (Mediarecorder.audiosouRce. MIC);//Read Microphone sound Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);//Set Output format Recorder.setaudioencoder ( MediaRecorder.AudioEncoder.AMR_NB)//Encoding mode file File=new file (Environment.getdownloadcachedirectory ().
GetAbsolutePath (), "recorder");
if (!file.exists ()) {File.mkdir (); Recorder.setoutputfile (File.getabsolutepath () + "/" + system.currenttimemillis () + "3gp");//storage location is placed in the SD card Recorder directory T
ry {recorder.prepare ();
Recorder.start ();
Recording=true;
catch (IOException e) {e.printstacktrace (); @Override public IBinder onbind (Intent Intent) {throw new unsupportedoperationexception (' not yet implemented ')
); }
}
Service XML configuration
<service
android:name= ". Listenphoneservice "
android:enabled=" true "
android:exported=" true ">
</service>
V. Finally, don't forget to set up some permissions
<!--add access to mobile phone status-->
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
<!--call permission-->
<uses-permission android:name= "Android.permission.CALL_PHONE"/>
<!--the right to monitor phone-->
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
<!--Create and delete file permissions in SDcard-->
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--Write data permissions to SDcard-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
OK, the above is the whole of this article, I hope to learn more about Android four basic components to help, thank you.