service-blacklist call automatically hangs up

Source: Internet
Author: User

This is just a learning routine, and many features need to be perfected

Allow users to dynamically load phone address Book phone number, tick some number as the blacklist, once some number is blacklisted, when the number automatically hang up the number

Android does not publicly hang up the phone API, if you need to hang up the phone, you must use Aidl to communicate with the telephone Management Service, and invoke the API in the service to achieve end calls

In order to invoke the remote Aidl Service, the two aidl files shown are required

The Blockmain.java interface provides a button for users to open a dialog box to check the blacklist,

public class Blockmain extends Activity {arraylist<string> blocklist = new arraylist<string> ();// Save blacklist phone arraylist<string> numlist = new arraylist<string> (); Save Address Book phone Telephonymanager tmanager; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main);//Get the Telephonymanager manager of the system Tmanager = (Telephonymanager) Getsystemservice (Telephony_service); Phonestatelistener listener = new Phonestatelistener () {@Overridepublic void oncallstatechanged (int state, String Incomingnumber) {switch (state) {case Telephonymanager.call_state_idle:break;case Telephonymanager.call_state_ offhook:break;//when the phone is called in case telephonymanager.call_state_ringing://if the number belongs to the blacklist if (Isblock (Incomingnumber)) { SYSTEM.OUT.PRINTLN ("* * Hang up the phone * *"); Try{method Method = Class.forName ("Android.os.ServiceManager"). GetMethod (" GetService ", string.class);//get proxy for remote Telephony_service IBinder object IBinder binder = (ibinder) method.invoke (NULL, new Object[]{telephony_SERVICE});//Convert the proxy of the IBinder object to the Itelephony object Itelephony telephony = ITelephony.Stub.asInterface (binder);// Hang up the phone Telephony.endcall ();} catch (Exception e) {e.printstacktrace ();}} break;} Super.oncallstatechanged (state, incomingnumber);}};/ /telephonymanager Monitor call status change Tmanager.listen (Listener, phonestatelistener.listen_call_state);//Get the program button, and for its Click event binding Listener Findviewbyid (R.id.managerblock). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {final Cursor cursor = getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI , NULL, NULL, NULL, or NULL); Baseadapter adapter = new Baseadapter () {@Overridepublic int getcount () {return Cursor.getcount ();} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview,viewgroup parent) {cursor.movetoposition (position); CheckBox RB = new checkbox (blockmain.this); String number = cursor.getstring (cursor.Getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)). Replace ("-", ""). Replace ("", ""); Rb.settext ( Numlist.add (number), or//if it has been blacklisted, by default tick the number if (Isblock (number)) {rb.setchecked (true);} Return rb;}};/ /load list.xml layout file corresponding to viewfinal View Selectview = Getlayoutinflater (). Inflate (r.layout.list, NULL);// Gets the ListView component named List in Selectview final listview listview = (ListView) Selectview.findviewbyid (r.id.list); Listview.setadapter (adapter); new Alertdialog.builder (Blockmain.this) Setview (Selectview). Setpositivebutton ("OK" , new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {// Empties the Blocklist Collection Blocklist.clear ();//iterates through each list item of the ListView component, which simply iterates through the current page int numofvisibleview = Listview.getlastvisibleposition ()-listview.getfirstvisibleposition (); for (int i = 0; i < Numofvisibleview; i++) { CheckBox checkbox = (checkbox) Listview.getchildat (i),//If the list item is checked if (checkbox.ischecked ()) {// Add the phone number of the list item Blocklist.add (Checkbox.gettext (). toString ());}}}). Show);}});} Determine if a phone number is within the blacklist public boolean isblock (String phone) {System.out.println ("Incoming number:" +phone); SYSTEM.OUT.PRINTLN ("-----" +blocklist); for (String s1:blocklist) {if (S1.equals (phone)) {return true;}} return false;}}

Mainfest files Configure appropriate permissions

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Crazyit.manager "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <!--grant the app permission to control calls--><uses-permission and Roid: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 permission to read contact ContentProvider--><uses-permission android:name= "Android.permission.READ_CONTACTS"/><!--Grant ContentProvider permission to write contacts--><uses-permission android:name= " Android.permission.WRITE_CONTACTS "/> <application android:allowbackup=" true "android:icon=" @drawab            Le/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android:name= "Crazyit.manager.BlockMain" Android: label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.act Ion. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte R> </activity> </application></manifest>


Note:

This routine can only check the number of the currently displayed page,

Numofvisibleview = Listview.getlastvisibleposition ()-listview.getfirstvisibleposition ();
This is the original routine to modify, the original routine if you encounter a lot of numbers (more than a page of mobile phones, you need to also swipe page) situation will be a bug

Specific circumstances such as http://www.eoeandroid.com/forum.php?mod=viewthread&tid=249149

There is no right way to find a solution on the internet,

For the moment, use Numofvisibleview to indicate the number of pages currently displayed, and add the phone number of this page to the blacklist.

(also found a solution, but did not modify success http://blog.csdn.net/fengyuzhengfan/article/details/38166967)


service-blacklist call automatically hangs up

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.