Android Development blacklist automatically hangs up and android blacklist

Source: Internet
Author: User
Tags add numbers

Android Development blacklist automatically hangs up and android blacklist

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


In this way, the corresponding. Java file will be generated under the gen folder.

In addition, for details about how to call an API, refer to Class. forName ().

In addition, BaseAdapter is used as the ListView adapter in this example. BaseAdapter has a strong custom type.

Demo image:


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: </br> * blacklist automatically hangs up * 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 <String> blockList = new ArrayList <String> (); @ 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

<! -- Grant the application permission to control calls --> <uses-permission android: name = "android. permission. CALL_PHONE"/> <! -- Grant the application permission to read the call status --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"/> <! -- Grant the read contact ContentProvider permission --> <uses-permission android: name = "android. permission. READ_CONTACTS"/> <! -- Grant the write contact ContentProvider permission --> <uses-permission android: name = "android. permission. WRITE_CONTACTS"/>




No one in the android phone blacklist, but when someone calls the phone, the system automatically hangs up the fast append score.

Is it installed with software such as 360 mobile guard or QQ Mobile Phone Manager? If so, you can enter the settings.
 
Android 23 system, find a software that automatically hangs up and chooses whether to call back

Laidianbao, go to the Android Market to download it. An icon will be added in the upper right corner after installation. you can press the button and set the callback interval, or you can add the automatic text message function after hanging up, very easy to use! Although not as much as you said, you can basically achieve your ideas.

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.