How do I intercept a call and automatically hang up when certain numbers are detected?
Use reflection technology to access the internal features of the Android SDK to hang up the phone
1. The OnReceive () method of the broadcast receiver class (INCALLRECEIVER) that intercepts incoming calls
1 Public voidOnReceive (FinalContext context,intent Intent) {2 //Get a phone management service to get phone status3Telephonymanager tm=(Telephonymanager) Context.getsystemservice (service.telephony_service);4 //processing According to the different call status5 Switch(Tm.getcallstate ()) {6 //Bells7 Casetelephonymanager.call_state_ringing:8 //get the phone number for the call9String Incomingnumber=intent.getstringextra ("Incoming_number");Ten //If the call number is 12345678. Hang up the phone One if("12345678". Equals (Incomingnumber)) { ACalss<telephonymanager> Telephonymanagerclass=telephonymanager.class; - //using Java Reflection technology to obtain the method object corresponding to the Getitelephony methods -Method Telephonymethod=telephonymanagerclass.getdelaredmethod ("Getitelephony", (class[])NULL); the //allow access to the Getitelephony method -Telephonymethod.setaccessible (true); - //call the Getitelephony method to get the Itelephony object -Object Obj=telephonymethod.invoke (Telephonymanager. Object[])NULL); + //gets the Endcall method corresponding to the methods object -Method Endcallmethod=obj.getclass (). GetMethod ("Endcall",NULL); + //allow access to the Endcall method AEndcallmethod.setaccessible (true); at //call the Endcall method to hang up the phone -Endcallmethod.invoke (obj,NULL); - - } - Break; - CaseTelephonymanager.call_state_offhook://answer the phone inLOG.D ("Call_state", "Offhook"); - Break; to CaseTelephonymanager.call_state_idle://Hang up the phone + closetoast (); - Break; the } *}
2. Finally, you need to define the broadcast receiver in the manifest file and add the permission to receive the call broadcast
Configure Incallreceiver
1 <receiver android:name= ". Incassreceiver "2 android:enable=" Treu ">3 <intent-=filter>4 < Action android:name= "Android.intent.action.PHONE_STATE"/>5 <intent-filter>6 </recevier>
Interception and judgment of incoming call information