How to create a blacklist

Source: Internet
Author: User

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

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.