The general principle of the blacklist is to listen to incoming calls, when a call is to check its number, whether it exists in the blacklist. If it is the number in the blacklist, hang up the phone immediately.
So the main step is four steps: First, listen to have a call and get its number; second, check whether the number is in the blacklist; third, hang up the telephone;
First, listen to the call and get its number
1. Introduction of interface Files
The file name and package name must be the same.
2. Permission to apply
<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission Android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= " Android.permission.CALL_PHONE "/> <uses-permission android:name=" Android.permission.READ_CONTACTS "/ >
3. Description of monitoring content
Customphonecalllistener Cplistener; Telephonymanager Telephonymanager; Public classCustomphonecalllistenerextendsphonestatelistener{@Override Public voidOncallstatechanged (intState , String Incomingnumber) { Switch(state) { CaseTelephonymanager.call_state_idle: Break; CaseTelephonymanager.call_state_offhook: Break; Casetelephonymanager.call_state_ringing:if(Isblock (Incomingnumber)) {Try{Method Method= Class.forName ("Android.os.ServiceManager"). GetMethod ("GetService", String.class); IBinder Binder= (IBinder) Method.invoke (NULL,NewObject[]{telephony_service}); Itelephony Telephony=ITelephony.Stub.asInterface (binder); Telephony.endcall (); } Catch(Exception e) {e.printstacktrace (); } } } Super. oncallstatechanged (State, Incomingnumber); } }
Where incomingnumber is the number of incoming calls.
Second, check whether the number is in the blacklist
Private Boolean Isblock (String number) { for (int i = 0; i < black_phone.size (); i++) {
9>if
(Black_phone.get (i). Equals (number) {
return
true
; } }
return
false
; }
Third, if the number exists in the blacklist, hang up the telephone
if(Isblock (Incomingnumber)) {Try{Method Method= Class.forName ("Android.os.ServiceManager"). GetMethod ("GetService", String.class); IBinder Binder= (IBinder) Method.invoke (NULL,NewObject[]{telephony_service}); Itelephony Telephony=ITelephony.Stub.asInterface (binder); Telephony.endcall (); } Catch(Exception e) {e.printstacktrace (); } }
Above
How to create a blacklist