Telephonymanager Management of The Blacklist

Source: Internet
Author: User

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;

Import Android.os.Bundle;
Import Android.os.IBinder;
Import android.os.RemoteException;
Import Android.provider.ContactsContract;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.database.Cursor;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.View.OnClickListener;
Import Android.widget.BaseAdapter;
Import Android.widget.CheckBox;
Import Android.widget.ListView;

public class Blockmain extends Activity {
List of record blacklist
arraylist<string> blocklist = new arraylist<string> ();
Telephonymanager Tmanager;
Listener listening on call status
Customphonecalllistener Cplistener;
public class Customphonecalllistener extends phonestatelistener{
@Override
public void oncallstatechanged (int state, String incomingnumber) {
Switch (state) {
Case Telephonymanager.call_state_idle:
Break
Case Telephonymanager.call_state_offhook:
Break
Phone call-In
Case telephonymanager.call_state_ringing:
If the number belongs to the blacklist
if (Isblock (Incomingnumber)) {
try {
method = Class.forName ("Android.os.ServiceManager")
. GetMethod ("GetService", String.class);
Gets the proxy for the remote Telephony_service IBinder object
IBinder binder = (ibinder) method.invoke (NULL,
New Object[]{telephony_service});
To convert a proxy for a IBinder object to a Itelephony object
Itelephony telephony = ITelephony.Stub.asInterface (binder);
Hang up the phone
Telephony.endcall ();
} catch (Nosuchmethodexception e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
} catch (InvocationTargetException e) {
E.printstacktrace ();
} catch (RemoteException e) {
E.printstacktrace ();
}
}

Default
Break
}
Super.oncallstatechanged (state, Incomingnumber);
}
}

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_block_main);
Get the system's Telephonymanager Manager
Tmanager = (Telephonymanager) getsystemservice (Context.telephony_service);
Cplistener = new Customphonecalllistener ();
Monitor the change of call status via Telephonymanager
Tmanager.listen (Cplistener, phonestatelistener.listen_call_state);
Gets the program's button and binds the listener for its Click event
Findviewbyid (R.id.managerblock). Setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Query the phone number of the contact person
Final cursor cursor = getcontentresolver (). Query (
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
NULL, NULL, NULL, or NULL);
Baseadapter adapter = new Baseadapter () {

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Cursor.movetoposition (position);
CheckBox RB = new checkbox (blockmain.this);
Get the phone number of the contact and remove the middle underline
String number = cursor.getstring (cursor
. Getcolumnindex (Contactscontract.
CommonDataKinds.Phone.NUMBER). Replace ("-", "" ");
Rb.settext (number);
If the number has been blacklisted, the number is checked by default
if (Isblock (number)) {
Rb.setchecked (TRUE);
}
return RB;
}

@Override
public long getitemid (int position) {
return position;
}

@Override
Public Object getItem (int position) {
return position;
}

@Override
public int GetCount () {
return Cursor.getcount ();
}
};
Loads the view of the List.xml layout file object
View Selectview = Getlayoutinflater (). Inflate (r.layout.list, NULL);
Gets the ListView component named List in the 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 () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
Emptying the Blocklist collection
Blocklist.clear ();
Iterate through each list item of a ListView component
for (int i = 0; I<listview.getcount (); 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 ());
}
}
System.out.println (blocklist);
}
}). Show ();
}
});
}

Determine if a phone number is in the blacklist
public boolean isblock (String phone) {
for (String s1:blocklist) {
if (s1.equals (phone)) {
return true;
}
}
return false;
}

}

Telephonymanager Management of The 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.