Android Development blacklist call automatically hangs up

Source: Internet
Author: User
Tags add numbers

This example allows users to dynamically add numbers to the blacklist, and implement the blacklist calls automatically hang up. The program implements this function by creating a Phonestatelistener listener to listen for Telephonymanager call status.

Since Android 10 is no longer publicly hung up on the phone API, if you need to hang up the phone must use Aidl to communicate with the phone Management service and invoke the API in the service to implement the end call.

In order to invoke the remote Aidl Service, the developer needs to copy the following two text from the Android source to the specified location:

Com.android.internal.telephony under the Bag Itelephony.aidl

Android.telephony under the Bag Neighboringcellinfo.aidl


This will generate the corresponding in the Gen folder. Java files.

Another way to call the API if there is a puzzled place can refer to: The role of Class.forName () and the use of summary

In addition, this example uses Baseadapter as the adapter for the ListView, 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 example implements the get contact and adds the selected contacts to the blacklist * and uses the aidl with the Itelephony.aidl and Neighboringcellinfo.aidl * interface to communicate with the phone management Servic hang up the phone in the blacklist * @ Author JPH * time:2014.07.24 * */public class Callguard extends Activity {Button btnmanage; Telephonymanager tmanager;//Create a collection to hold the number in the blacklist arraylist<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 (Telephony_service);//Create phone status listener Phonestatelistener pstatelistener=new Phonestatelistener () {@ overridepublic void oncallstatechanged (int state, String incomingnumber) {//TODO auto-generated method Stubswitch (state {Case telephonymanager.call_state_idle://idle state does not handle break;case telephonymanager.call_state_offhook://pick up phone do not handle break Case telephonymanager.call_state_ringing://is ringing//if the caller number is automatically hung up in the Blacklist if (Isblock (Incomingnumber)) {try {// Gets the GetService () method of the object of the Android.os.ServiceManager class, Method=class.forname ("Android.os.ServiceManager"). GetMethod ("GetService", string.class);//Gets the proxy IBinder object of the remote Telephony_service. Binder = (IBinder) IBinder ( NULL, new object[] {telephony_service});//Convert IBinder object's proxy to Itelephony object Itelephony telephony= ITelephony.Stub.asInterface (Binder);//Hang Up the phone telephony.endcall (); } catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} break;} Super.oncallstatechanged (state, incomingnumber);}};/ /Add Listener Tmanager.listen for Telephonymanager (Pstatelistener, phonestatelistener.listen_call_state); Btnmanage.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//Query the phone number in the communication record final Cursor cursor=getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_ URI, NULL, NULL, NULL),//Create a Baseadapter as a ListView adapter Baseadapter adapter=new Baseadapter () {@Overridepublic View GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method stub//Moves the pointer of the cursor to the specified position CURSOR.M Ovetoposition (position); CheckBox box=new checkbox (Callguard.this);//Gets the phone number at the specified location string number=cursor.getstring (Cursor.getcolumnindex ( ContactsContract.CommonDataKinds.Phone.NUMBER));//Remove the middle dash, Space number.replace ("-", "") between the phone numbers. Replace ("", ""); Box.settext (number);//If the numbers have been added to the blacklist, by default tick the number 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;} Returns the total number of bars in the list @overridepublic int GetCount () {//TODO auto-generated method Stubreturn cursor.getcount ();}};/ /Load Line layout file View View=getlayoutinflater (). Inflate (r.layout.line, NULL);//Get the component with ID list in the line layout file final ListView list= ( ListView) View.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++) {Chec Kbox box= (CheckBox) List.getchildat (i);//If the contact is selected, add it to the Blocklist collection if (box.ischecked ()) {Blocklist.add (Box.gettext ( ). ToString ());}}}). Show ();}}); Private Boolean Isblock (StrinG number) {//TODO auto-generated method stub//Determine whether the numbers are in the Blocklist collection for (String s:blocklist) {if (s.equals (number)) {return true;}} return false;}}

Finally, don't forget to add the appropriate permissions:

Androidmanifest.xml

<!--grant the app the right to control calls--><uses-permission android:name= "Android.permission.CALL_PHONE"/><!-- Grant the app the right to read the call status--><uses-permission android:name= "Android.permission.READ_PHONE_STATE"/><!-- Grant access to read contact ContentProvider--><uses-permission android:name= "Android.permission.READ_CONTACTS"/><!-- Grant Write contact ContentProvider permissions--><uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>



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.