Android 实现电话号码(接听/拨号/短信)白名单

Android 实现电话号码(接听/拨号/短信)白名单

我遇到一个需求,需要实现电话号码的白名单,除了通讯录里的号码,所有来电短信都要屏蔽掉。

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
   /**
* Returns {@code true} if {@code phoneNumber} is blocked.
* <p>
* This method catches all underlying exceptions to ensure that this method never throws any
* exception.
*/
public static boolean isBlocked(Context context, String phoneNumber) {
boolean isBlocked = false;
long startTimeNano = System.nanoTime();

try {
+ //注释掉这部分原生判断,会读取BlockedNumberContract数据库里的名单作对比
+ // if (BlockedNumberContract.SystemContract.shouldSystemBlockNumber(
+ // context, phoneNumber)) {
+ // Rlog.d(TAG, phoneNumber + " is blocked.");
+ // isBlocked = true;
+ // }

+ //新增一个shouldBlockNumber函数,这里可以根据自己需求,从什么数据库读取白名单来判断对应号码是否该拦截
+ 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());
+ // not block
+ // block = BlockChecker.isBlocked(mContext, tracker.getDisplayAddress());


- block = BlockChecker.isBlocked(mContext,
- cursor.getString(PDU_SEQUENCE_PORT_PROJECTION_INDEX_MAPPING
- .get(DISPLAY_ADDRESS_COLUMN)));

+ // not block
+ // 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())) {
+
+ // not block
+ // && 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
    /**
* Processes CALL, CALL_PRIVILEGED, and CALL_EMERGENCY intents.
*
* @param intent Call intent containing data about the handle to call.
*/
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;
+ }


}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×