Automatic suspension of incoming calls in the blacklist of Android Development

Source: Internet
Author: User
Tags add numbers

Automatic suspension of incoming calls in the blacklist of Android Development

This instance allows you to dynamically add numbers to the blacklist and automatically stop blacklisted calls. The program creates a PhoneStateListener listener to monitor the call status of TelephonyManager.

Since Android 10 and later do not open APIs for hanging up the phone, you must use AIDL to communicate with the phone Management Service and call the API in the Service to end the call.

To call the remote AIDL Service, the developer needs to copy the following two files in the Android source code to the specified location:

ITelephony. aidl under com. android. internal. telephony

NeighboringCellInfo. aidl In the android. telephony package


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1eLR + b7Nu + release/Cyfqzyc/g06a1xC5KYXZhzsS8/release/ydLUss6/release + we3N4rG + yrXA/release + release = "http://www.2cto.com/uploadfile/Collfiles/20140726/20140726091337155.gif" alt = "\">

Program code:

Package com. jph. callguard; import java. lang. reflect. method; import java. util. arrayList; import com. android. internal. telephony. ITelephony; import android. OS. bundle; import android. OS. IBinder; import android. provider. contactsContract; import android. telephony. phoneStateListener; import android. telephony. telephonyManager; import android. view. view; import android. view. viewGroup; import android. view. view. onClickListener; import android. widget. baseAdapter; import android. widget. button; import android. widget. checkBox; import android. widget. listView; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. database. cursor;/*** Describe:
* Automatically hangs up in the blacklist * This instance obtains contacts, adds the selected contacts to the blacklist *, and uses ITelephony. aidl and NeighboringCellInfo. the aidl * interface uses AIDL to communicate with the telephone management Servic and hangs up the phone in the blacklist * @ author jph * Time: 2014.07.24 **/public class CallGuard extends Activity {Button btnManage; TelephonyManager tManager; // create a set to save the number ArrayList in the blacklist BlockList = new ArrayList (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); btnManage = (Button) findViewById (R. id. btnManage); tManager = (TelephonyManager) getSystemService (telephone); // create a telephone status listener listen pStateListener = new PhoneStateListener () {@ Overridepublic void onCallStateChanged (int state, String incomingNumber) {// TODO Auto-generated method stubswitch (state) {case TelephonyManager. CALL_STATE_IDLE: // do not process break in idle state; case TelephonyManager. CALL_STATE_OFFHOOK: // do not process break when receiving a call; case TelephonyManager. CALL_STATE_RINGING: // ringing // if the incoming call number is in the blacklist, if (isBlock (incomingNumber) is automatically hung up {try {// get android. OS. the getService () Method method of the ServiceManager Class Object = Class. forName ("android. OS. serviceManager "). getMethod ("getService", String. class); // obtain the proxy IBinder binder = (IBinder) method of the IBinder object of the remote TELEPHONY_SERVICE. invoke (null, new Object [] {TELEPHONY_SERVICE}); // convert the proxy of the IBinder Object to the ITelephony Object ITelephony telephony = ITelephony. stub. asInterface (binder); // call telephony. endCall ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} break;} super. onCallStateChanged (state, incomingNumber) ;}}; // adds a listener tManager for TelephonyManager. listen (pStateListener, PhoneStateListener. LISTEN_CALL_STATE); btnManage. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // query the phone number final Cursor cursor = getContentResolver () in the contact list (). query (ContactsContract. commonDataKinds. phone. CONTENT_URI, null, null); // create a BaseAdapter as the ListView adapter BaseAdapter = new BaseAdapter () {@ Overridepublic View getView (int position, View convertView, viewGroup parent) {// TODO Auto-generated method stub // move the cursor pointer to the specified position cursor. moveToPosition (position); CheckBox box = new CheckBox (CallGuard. this); // obtain the phone number at the specified location String number = cursor. getString (cursor. getColumnIndex (ContactsContract. commonDataKinds. phone. NUMBER); // remove the strip and space number in the middle of the phone NUMBER. replace ("-",""). replace ("", ""); box. setText (number); // if the number has been blacklisted, this number is selected by default if (isBlock (number) {box. setChecked (true);} return box;} @ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position;} @ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn position;} // The total number of items in the returned list @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn cursor. getCount () ;}}; // load the line layout file View = getLayoutInflater (). inflate (R. layout. line, null); // obtain the final ListView list = (ListView) view component whose Id is list in the line layout file. findViewById (R. id. list); list. setAdapter (adapter); new AlertDialog. builder (CallGuard. this ). setView (view ). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubblockList. clear (); for (int I = 0; I <list. getCount (); I ++) {CheckBox box = (CheckBox) list. getChildAt (I); // if the contact is selected, add it to the blockList set if (box. isChecked () {blockList. add (box. getText (). toString ());}}}}). show () ;}}) ;}private boolean isBlock (String number) {// TODO Auto-generated method stub // determine whether the number is in the blockList set for (String s: blockList) {if (s. equals (number) {return true ;}} return false ;}}

Finally, do not forget to add the corresponding permissions:

AndroidManifest. xml

 
 
 
 
 
 
 
 



Related Article

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.