The implementation method of blacklist in Android programming _android

Source: Internet
Author: User
Tags reflection stub

This example describes the implementation of blacklist in Android programming. Share to everyone for your reference, specific as follows:

Description: Because the phone hangs up the Android API is not open to the outside, you need to use the method of reflection to get the call service.

1. Copy "Aidl" files from the Android source code into the project

Two packages are generated in this project: Android.telephony; The file in this package is: Neighboringcellinfo.aidl

Com.android.internal.telephony the file in this package is: Itelephony.aidl

2. Hang up the phone by reflection; The code is as follows:

/**
* Hang up the phone/public
void Endcall () {
    try {method method
      = Class.forName (" Android.os.ServiceManager "). GetMethod (" GetService ", string.class);
      IBinder binder = (ibinder) method.invoke (null, New Object[]{telephony_service});
      Itelephony telephony = ITelephony.Stub.asInterface (binder);
      Telephony.endcall ();
    } catch (Exception e) {
      e.printstacktrace ();
    }
}

3. Delete the records in the call log

/**
* Delete Call log
/public void Deletecalllog (String incomingnumber) {
    Contentresolver resolver = Getcontentresolver ();
    Cursor Cursor = resolver.query (CallLog.Calls.CONTENT_URI, NULL, "Number=?", New String[]{incomingnumber}, NULL);
    if (Cursor.movetonext ()) {
      String id = cursor.getstring (cursor.getcolumnindex ("_id"));
      Resolver.delete (CallLog.Calls.CONTENT_URI, "_id=", New String[]{id});}


4. This call is not allowed to delete phone records, because the process of generating phone records is an asynchronous process, after hanging up the phone can not immediately delete the phone records, so here to use CONTENTOBSERVER (content observer)

Private class Myobserver extends contentobserver{
    private String incomingnumber;
    Public Myobserver (Handler handler,string incomingnumber) {
      super (Handler);
      This.incomingnumber = Incomingnumber;
    }
    @Override public
    void OnChange (Boolean selfchange) {
      super.onchange (selfchange);
      Deletecalllog (incomingnumber);
      Getcontentresolver (). Unregistercontentobserver (this);
    }


6. Finally put the entire service code below

public class Addressservice extends service{private static final String TAG = "Addressservice";
  Private Telephonymanager Manager;
  Private Myphonelistener listener;
  Private WindowManager Wmanager;
  Private view view;
  Private Sharedpreferences sp;
  Long starttime = 0;
  Long endtime = 0;
  Private Blacknumberdao DAO;
  @Override public IBinder onbind (Intent arg0) {return null;
    /** * The method invoked when the service was first created * * The method called when the service was initialized * * * @Override public void OnCreate () {super.oncreate ();
    Listener = new Myphonelistener ();
    Manager = (Telephonymanager) getsystemservice (Telephony_service);
    Wmanager = (WindowManager) this.getsystemservice (Window_service);
    Manager.listen (Listener, phonestatelistener.listen_call_state);
    SP = getsharedpreferences ("config", mode_private);
DAO = new Blacknumberdao (this); if (3000> (endtime-starttime)) {//String NS = Context.notification_service;//Notificationmanager Mnotif Icationmanager = (notificAtionmanager) Getsystemservice (NS);
Define the content information of the notification bar//int icon = R.DRAWABLE.ICON5;
Charsequence Tickertext = "My notice bar title";
Long when = System.currenttimemillis ();
Notification Notification = new Notification (icon, Tickertext, when);
Define the content information to be displayed when the Drop-down notification bar/context = Getapplicationcontext ();
Charsequence Contenttitle = "My notice bar expands title";
Charsequence ContentText = "My notice bar expands the details";
Intent notificationintent = new Intent (addressservice.this,bootstartdemo.class);
Pendingintent contentintent = pendingintent.getactivity (addressservice.this, 0,notificationintent, 0);
Notification.setlatesteventinfo (context, contenttitle, contenttext,contentintent);
Using Mnotificationmanager notify method to inform the user to generate the title bar message notification//mnotificationmanager.notify (1, notification);
    }/** * When the service is stopped call/@Override public void OnDestroy () {Super.ondestroy ();
    Manager.listen (Listener, Phonestatelistener.listen_none); LisTener = null; Private class Myphonelistener extends phonestatelistener{/** * The method called when the status of the phone is changed/* @Override PU
      Blic void oncallstatechanged (int state, String incomingnumber) {super.oncallstatechanged (state, Incomingnumber); Switch (state) {case TelephonyManager.CALL_STATE_IDLE:if (null!= view) {Wmanager.removeview
          (view);
        view = null;
        } Endtime = System.currenttimemillis ();
      Break
          Case telephonymanager.call_state_ringing://0 Sound status//Determine whether number is in the blacklist if (Dao.find (Incomingnumber)) {
          Hang up the phone endcall ();
          Delete Call Log//Deletecalllog (Incomingnumber);
          Contentresolver resolver = Getcontentresolver ();
        Resolver.registercontentobserver (CallLog.Calls.CONTENT_URI, True, new Myobserver (New Handler (), incomingnumber));
        LOG.I (TAG, "caller number is" + incomingnumber); String address = numberaddressservice.getaddress (incomIngnumber);
        LOG.I (TAG, "belong to" + address);
        Showlocation (address);
        Gets the time of the current system starttime = System.currenttimemillis ();
      Break
      Case Telephonymanager.call_state_offhook://Connect call State break; /** * Displays the location information on the form * @param address/public void showlocation (String address) {Windowmanag Er.
    Layoutparams params = new Windowmanager.layoutparams ();
    Params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    Params.width = WindowManager.LayoutParams.WRAP_CONTENT; Params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    Params.format = pixelformat.translucent;
    Params.type = WindowManager.LayoutParams.TYPE_TOAST;
    Params.settitle ("Toast"); params.gravity = Gravity.left |
    Gravity.top;
    int x = Sp.getint ("Lastx", 0);
    int y = sp.getint ("Lasty", 0);
    params.x = x; Params. y = y;
    View = View.inflate (Getapplicationcontext (), r.layout.show_location, NULL);
    LinearLayout ll_location = (linearlayout) View.findviewbyid (r.id.ll_location);
    TextView tv_location = (TextView) View.findviewbyid (r.id.tv_location);
    int background = Sp.getint ("background", 0);
    if (0 = background) {Ll_location.setbackgroundresource (R.drawable.call_locate_gray);
    }else if (1 = background) {Ll_location.setbackgroundresource (r.drawable.call_locate_orange);
    }else {ll_location.setbackgroundresource (r.drawable.call_locate_green);
    Tv_location.settext (address);
    Tv_location.settextsize (24);
  Wmanager.addview (view, params); /** * Delete Call Log/public void Deletecalllog (String incomingnumber) {contentresolver resolver = Getcontentre
    Solver ();
    Cursor Cursor = resolver.query (CallLog.Calls.CONTENT_URI, NULL, "Number=?", New String[]{incomingnumber}, NULL); if (Cursor.movetonext ()) {String id = cursor.getstring (cUrsor.getcolumnindex ("_id"));
    Resolver.delete (CallLog.Calls.CONTENT_URI, "_id=", New String[]{id}); }/** * Hang up phone/public void Endcall () {try {method method = Class.forName ("Android.os.ServiceMan
      Ager "). GetMethod (" GetService ", String.class);
      IBinder binder = (ibinder) method.invoke (null, New Object[]{telephony_service});
      Itelephony telephony = ITelephony.Stub.asInterface (binder);
    Telephony.endcall ();
    catch (Exception e) {e.printstacktrace ();
    } private class Myobserver extends contentobserver{private String incomingnumber;
      Public Myobserver (Handler handler,string incomingnumber) {super (Handler);
    This.incomingnumber = Incomingnumber;
      @Override public void OnChange (Boolean selfchange) {Super.onchange (selfchange);
      Deletecalllog (Incomingnumber);
    Getcontentresolver (). Unregistercontentobserver (this);

 }
  }
}

More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Communication Summary", "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.