Android来电拦截的实现方法
作者:Saflyer 时间:2023-10-09 16:42:20
本文实例为大家分享了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,来电拦截
0
投稿
猜你喜欢
java如何去除图片中的白色背景
2022-04-17 00:07:14
mybatis映射和实际类型不一致的问题
2023-08-13 18:09:06
java编程多线程并发处理实例解析
2022-06-02 22:14:59
Android实现抽奖转盘实例代码
2021-08-22 20:03:35
C#中的char与string详解
2023-09-13 07:07:27
SpringBoot整合Elasticsearch7.2.0的实现方法
2023-11-09 19:22:56
C#读取或设置ScrollLock状态的方法
2023-03-14 21:41:24
C# 泛型集合的自定义类型排序的实现
2022-11-18 20:35:32
Maven依赖作用域和依赖传递的使用
2022-07-24 19:08:33
Spring+MyBatis实现数据读写分离的实例代码
2021-08-31 04:34:48
Java封装的实现访问限定符、包
2023-03-20 07:08:48
Android 调用系统相机拍摄获取照片的两种方法实现实例
2022-01-19 21:22:23
从零开始Java实现Parser Combinator
2023-06-18 18:52:04
Java8简单了解Lambda表达式与函数式接口
2022-11-07 00:22:31
Android下载进度监听和通知的处理详解
2022-03-27 08:39:02
SpringMVC整合mybatis实例代码
2022-09-24 08:22:03
mybatis createcriteria和or的区别说明
2021-09-27 09:20:40
Visual C#类的定义及实现方法实例解析
2023-06-04 18:10:37
Java Swing中的工具栏(JToolBar)和分割面版(JSplitPane)组件使用案例
2021-09-07 14:45:17
Java Unsafe类实现原理及测试代码
2023-03-10 21:18:13