我遇到一个需求,需要实现电话号码的白名单,除了通讯录里的号码,所有来电短信都要屏蔽掉。
Android 7.0后提供了一个电话号码黑名单的功能,可以拦截对应号码的电话和短信,即Android N BlockedNumberContract原生黑名单。
所以我这里就直接利用这个模块的函数,再此之上增加白名单判断。
白名单电话接听拦截和短信拦截 这里直接利用BlockedNumberContract框架里的关键判断类BlockChecker,BlockChecker的isBlocked函数会返回一个bool值,根据这个bool值,BlockedNumberContract框架会决定是否拦截对应号码的电话和短信。
修改位置 frameworks\opt\telephony\src\java\com\android\internal\telephony\BlockChecker.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public static boolean isBlocked (Context context, String phoneNumber) { boolean isBlocked = false ; long startTimeNano = System.nanoTime(); try { + + + + + + + + if (shouldBlockNumber( context, phoneNumber)) { + Log.d(TAG, phoneNumber + " is blocked." ); + isBlocked = true ; + } } catch (Exception e) { Rlog.e(TAG, "Exception checking for blocked number: " + e); } int durationMillis = (int ) ((System.nanoTime() - startTimeNano) / 1000000 ); if (durationMillis > 500 || VDBG) { Rlog.d(TAG, "Blocked number lookup took: " + durationMillis + " ms." ); } return isBlocked; }
如果不想短信受控制,可以修改 frameworks/opt/telephony/src/java/com/android/internal/telephony/InboundSmsHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 - block = BlockChecker.isBlocked(mContext, tracker.getDisplayAddress()); + + - block = BlockChecker.isBlocked(mContext, - cursor.getString(PDU_SEQUENCE_PORT_PROJECTION_INDEX_MAPPING - .get(DISPLAY_ADDRESS_COLUMN))); + + + + ``` frameworks/opt/telephony/src/java/com/android/internal/telephony/WapPushOverSms.java ``` - if (nInd.getFrom() != null - && BlockChecker.isBlocked(mContext, nInd.getFrom().getString())) { + + + + if (nInd.getFrom() != null ) {
电话拨号拦截 修改位置 proprietary/packages/services/Telecomm/src/com/android/server/telecom/CallIntentProcessor.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 static void processOutgoingCallIntent ( Context context, CallsManager callsManager, Intent intent) { if (phoneNumber != null ) { if (!PhoneNumberUtils.isUriNumber(phoneNumber)) { phoneNumber = PhoneNumberUtils.convertAndStrip(phoneNumber); } } + if (shouldBlockNumber(context,phoneNumber)) { + + Log.d(CallIntentProcessor.class,phoneNumber + " is blocked."); + + return ; + } }