The realization method of Android call interception _android

Source: Internet
Author: User

This example for you to share the Android call interception method for your reference, the specific content as follows

Permissions

  <uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
  <uses-permission android:name = "Android.permission.CALL_PHONE"/>
  <uses-permission android:name= "Android.permission.PROCESS_OUTGOING _calls "/>

 <!--registered broadcast monitor dial out phone-->
 <receiver android:name=" Com.example.administrator.endcall.PhoneStateReceiver ">
      <intent-filter>
        <action android:name = "Android.intent.action.PHONE_STATE"/>
        <action android:name= "Android.intent.action.NEW_OUTGOING_CALL" />
      </intent-filter>
 </receiver>

dial-up Broadcast -phonestatereceiver

Package com.example.administrator.endcall;

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;
Import Android.util.Log;

The public class Phonestatereceiver extends Broadcastreceiver {
  @Override
  the public void onreceive Intent Intent) {
    if (intent.getaction (). Equals (Intent.action_new_outgoing_call)) {
      String PhoneNumber = Intent.getstringextra (intent.extra_phone_number);
      LOG.I ("Blockcallhelper", "Blockcallhelper------->>>>" + PhoneNumber);
    }
  }


Call Hang Up
Blockcallhelper

Package com.example.administrator.endcall;
Import Android.content.Context;
Import android.content.SharedPreferences;
Import Android.database.Cursor;
Import Android.net.Uri;
Import android.os.RemoteException;
Import Android.telephony.PhoneStateListener;
Import Android.telephony.TelephonyManager;

Import Android.util.Log;

Import Com.android.internal.telephony.ITelephony;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;

Import java.util.List;

  Public final class Blockcallhelper {private static final String TAG = "Blockcallhelper";

  Private context Mcontext;

  Private Telephonymanager Tmanger;

  Private list<string> phones;

  Private Blockcallback BCB;  Private static Final class Factory {private static final Blockcallhelper
  Instance = new Blockcallhelper ();
  public static Blockcallhelper getinstance () {return factory.instance; }/** * InitializationBelow as well as data * @param context */public blockcallhelper init (contextual context) {if (contexts = null) {throw n
    EW NullPointerException ("context = = null!");
    } This.mcontext = context;
    This.tmanger = (Telephonymanager) mcontext.getsystemservice (Context.telephony_service);
    Tmanger.listen (New Phonecalllistener (), phonecalllistener.listen_call_state);
  return this; /** * Inject the cell phone number that needs to be intercepted * @param phonel * * * public blockcallhelper injectblockphonenum (arraylist<string> blo
    Ckcalls) {this.phones = Blockcalls;
  return this;
    /** * End call/private void Endcall () {class<telephonymanager> TMC = Telephonymanager.class;
    Method Getitelephonymethod;
      try {Getitelephonymethod = Tmc.getdeclaredmethod ("Getitelephony", (class[)) null);
      Getitelephonymethod.setaccessible (TRUE);
      Itelephony itelephony = (itelephony) Getitelephonymethod.invoke (Tmanger, (object[)) null);
    Itelephony.endcall (); } CatCH (nosuchmethodexception E1) {e1.printstacktrace ();
    catch (invocationtargetexception E2) {e2.printstacktrace ();
    catch (Illegalaccessexception E3) {e3.printstacktrace ();
    catch (remoteexception e4) {e4.printstacktrace (); } Private Final class Phonecalllistener extends Phonestatelistener {@Override public void Oncallstatechang Ed (int state, String incomingnumber) {if (state = = telephonymanager.call_state_ringing) {if (Phones.contai
          NS (Incomingnumber)) {log.i ("Blockcallhelper", "contains contains contains");
          Endcall ();
          if (BCB!= null) {bcb.callback (incomingnumber);
        } else {log.i ("Blockcallhelper", "contains not-------");
    Blockcallhelper Setblockcallback (Blockcallback back) {THIS.BCB = back;
  return this;
  public interface Blockcallback {void CallBack (String incomingnum);

 }
}

Look at the main interface mainactivity

 package com.example.administrator.endcall;
Import android.app.Activity;
Import Android.app.NotificationManager;
Import Android.os.Bundle;
Import Android.util.Log;

Import Android.widget.Toast;

Import java.util.ArrayList; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super
    . OnCreate (Savedinstancestate);
    Setcontentview (R.layout.activity_main);
    arraylist<string> blockcalls = new ArrayList ();
    Blockcalls.add ("13581922339");
    Blockcalls.add ("+8613581922339");
    Blockcalls.add ("18500813370");
    Blockcalls.add ("13717717622");

    Blockcalls.add ("+8613717717622"); Blockcallhelper.getinstance (). Init (This). Injectblockphonenum (Blockcalls). Setblockcallback (New Blockcallhelper.blockcallback () {@Override public void CallBack (String incomingnum) {log.i ("blockcal
      Lhelper "," Incomingnum-----------"+ incomingnum);
  }
    }); }
}

Last look at the aidl level
Itelephony.aidl

Package com.android.internal.telephony;
   Interface Itelephony {void dial (String number);
   void Call (String number);
   Boolean Showcallscreen ();
   Boolean Showcallscreenwithdialpad (Boolean showdialpad);
   Boolean Endcall ();
   void Answerringingcall ();
   void Silenceringer ();
   Boolean Isoffhook ();
   Boolean isringing ();
   Boolean isidle ();
   Boolean Isradioon ();
   Boolean issimpinenabled ();
   void Cancelmissedcallsnotification ();
   Boolean supplypin (String pin);
   Boolean Handlepinmmi (String dialstring);
   void Toggleradioonoff ();
   Boolean Setradio (Boolean turnon);
   void Updateservicelocation ();
   void Enablelocationupdates ();
   void Disablelocationupdates ();
   int Enableapntype (String type);
   int Disableapntype (String type);
   Boolean enabledataconnectivity ();
   Boolean disabledataconnectivity ();

   Boolean isdataconnectivitypossible ();
   Bundle getcelllocation ();

    List<neighboringcellinfo> Getneighboringcellinfo ();
   int getcallstate (); int getdataactivity ();
int getdatastate ();
 }

Neighboringcellinfo.aidl

Neighboringcellinfo.aidl
package android.telephony;
Parcelable Neighboringcellinfo;

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.