欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android来电拦截的实现方法

程序员文章站 2024-03-03 22:10:04
本文实例为大家分享了android来电拦截的方法,供大家参考,具体内容如下 权限

本文实例为大家分享了android来电拦截的方法,供大家参考,具体内容如下

权限

  <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" />

 <!-- 注册广播 监听拨出电话 -->
 <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>

拨号广播—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;

public class phonestatereceiver extends broadcastreceiver {
  @override
  public void onreceive(context context, intent intent) {
    if (intent.getaction().equals(intent.action_new_outgoing_call)) {
      string phonenumber = intent.getstringextra(intent.extra_phone_number);
      log.i("blockcallhelper", "blockcallhelper------->>>>" + phonenumber);
    }
  }
}


来电挂断
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;
  }

  /**
   * 初始化上下文以及数据
   * @param context
   */
  public blockcallhelper init(context context) {
    if (context == null) {
      throw new nullpointerexception("context == null!");
    }

    this.mcontext = context;
    this.tmanger = (telephonymanager) mcontext.getsystemservice(context.telephony_service);
    tmanger.listen(new phonecalllistener(), phonecalllistener.listen_call_state);
    return this;
  }

  /**
   * 注入需要拦截的手机号
   * @param phonel
   */
  public blockcallhelper injectblockphonenum(arraylist<string> blockcalls) {
    this.phones = blockcalls;
    return this;
  }
  /**
   * 结束通话
   */
  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 oncallstatechanged(int state, string incomingnumber) {
      if (state == telephonymanager.call_state_ringing) {
        if (phones.contains(incomingnumber)) {
          log.i("blockcallhelper", "contains contains contains");
          endcall();
          if (bcb != null) {
            bcb.callback(incomingnumber);
          }
        } else {
          log.i("blockcallhelper", "contains not-------");
        }
      }
    }
  }

  public blockcallhelper setblockcallback(blockcallback back) {
    this.bcb = back;
    return this;
  }

  public interface blockcallback {
    void callback(string incomingnum);
  }
}

看主界面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("blockcallhelper", "incomingnum-----------" + incomingnum);
      }
    });
  }
}

最后看aidl层面
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;

Android来电拦截的实现方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。