android 15,写一个防诈骗电话apk,源代码
开发一个针对 Android 15 (API Level 35) 的防诈骗电话拦截 App,需要通过注册 CallScreeningService 来在系统层面拦截并挂断电话。
Table of Contents
- 开发步骤
- step 3.create a service - AntiScamCallService.java
- step 4. register service - AndroidManifest.xml Configuration - register the AntiScamCallService service in your manifest
- step 5. Requesting the Role (Runtime) - MainActivity.java - request the ROLE_CALL_SCREENING role via the RoleManager at runtime.
- step 6.Android 15 Background execution rules
- 在 GitHub 上添加 APK 下载
developer tools:Android Studio Rabbit 1 | 2026.2.1
developer lan: java
开发步骤
In Android 15 (API level 35), using the CallScreeningService in Java requires a strict setup combining system roles, manifest configuration, and background execution requirements.
CallScreeningService in Java for Android 15 (API level 35), you must register the service in your manifest, implement the service class to handle and respond to calls within a strict 5-second window, and request the ROLE_CALL_SCREENING role via the RoleManager at runtime.
step 1.在 Android Studio 中新建一个 Empty Views Activity 项目
Empty Views Activity:传统的 java 开发模式。
step 2.test build project
x:\x\AntiScamBlocker\app\build\outputs\apk\debug
step 3.create a service - AntiScamCallService.java
path:app -> src -> main -> java -> com -> example -> antiscamblocker
Right-click on the com.example.antiscamblocker folder, New Java Class , name it AntiScamCallService.
package com.example.antiscamblocker;
import android.net.Uri;
import android.telecom.Call;
import android.telecom.CallScreeningService;
import androidx.annotation.NonNull;
/**
* 电话拦截程序
*/
public class AntiScamCallService extends CallScreeningService {
@Override
public void onScreenCall(@NonNull Call.Details callDetails) {
// Step 3.1: Check if the call is incoming
if (callDetails.getCallDirection() == Call.Details.DIRECTION_INCOMING) {
// Step 3.2: Extract the incoming phone number
Uri handle = callDetails.getHandle();
String phoneNumber = (handle != null) ? handle.getSchemeSpecificPart() : "";
// Step 3.3: Validate against anti-scam logic
if (isScamNumber(phoneNumber)) {
// Execute the intercept block and hang up
blockAndRejectCall(callDetails);
return;
}
}
// Pass-through: Allow normal calls to ring through smoothly
respondToCall(callDetails, new CallResponse.Builder().build());
}
/**
* Define your anti-fraud filter logic here.
*/
private boolean isScamNumber(String number) {
if (number == null || number.isEmpty()) {
return false;
}
// Example Rule 1: Block known dummy scam prefixes (e.g., +18005550199)
if (number.startsWith("+1800555")) {
return true;
}
// Example Rule 2: Block international calls posing as local numbers or suspicious lengths
// Customize this placeholder matching with actual local telecom scam patterns
return number.contains("scam-pattern-here");
}
/**
* Sends the execution command back to Android 15 to completely reject the call.
*/
private void blockAndRejectCall(Call.Details callDetails) {
CallResponse.Builder response = new CallResponse.Builder();
// 1. Disallow the incoming call from ringing
response.setDisallowCall(true);
// 2. Do not log this unwanted call in the system call log (Optional, cleaner user experience)
response.setSkipCallLog(true);
// 3. Prevent the system from firing a "missed call" notification
response.setSkipNotification(true);
// Instruct the OS Telecom system to process this execution response
respondToCall(callDetails, response.build());
}
}
step 4. register service - AndroidManifest.xml Configuration - register the AntiScamCallService service in your manifest
you must register the service in your manifest
Your service must be exported, protected by BIND_SCREENING_SERVICE
<service
android:name=".MyCallScreeningService"
android:permission="android.permission.BIND_SCREENING_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />
</intent-filter>
</service>
step 5. Requesting the Role (Runtime) - MainActivity.java - request the ROLE_CALL_SCREENING role via the RoleManager at runtime.
The system will not bind to your service unless your app holds the RoleManager.ROLE_CALL_SCREENING. You must request this from the user via the system dialog.
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private final ActivityResultLauncher<Intent> roleResultLauncher =
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK) {
// Role granted successfully
} else {
// Role denied by user
}
});
private void requestCallScreeningRole() {
RoleManager roleManager = (RoleManager) getSystemService(Context.ROLE_SERVICE);
if (roleManager != null && !roleManager.isRoleHeld(RoleManager.ROLE_CALL_SCREENING)) {
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_SCREENING);
roleResultLauncher.launch(intent);
}
}
}
step 6.Android 15 Background execution rules
To ensure reliability on Android 15, keep these platform behaviors in mind:
Strict Timeouts: Your service has roughly 5 seconds to execute respondToCall. If you are hitting a remote API to check spam scores, you must use an asynchronous HTTP client with a strict timeout wrapper. If your code hangs, the system will bypass your app and let the call through.
No Foreground Service Required: The system manages the lifecycle of this service entirely. Do not try to call startForeground() inside onScreenCall unless you are explicitly transitioning to a separate feature (like a custom overlay UI).
Data Access Restrictions:
If your app is restricted by Android 15's background data hygiene rules, ensure any local databases (like Room/SQLite) used for blacklists are initialized and cached before the call event occurs.
Deploy Debug APK in Android Studio
在 GitHub 上添加 APK 下载
step 1.new Publish release
step 2.上传 APK
在下方的 Attach binaries by dropping them here or selecting them 区域,将你的 .apk 文件拖拽进去(或者点击选择文件上传)
step 3.Publish release
CallShield (开源智能拦截库)
https://github.com/SysAdminDoc/CallShield
Calls Blocker (离线规则过滤)
https://github.com/ryosoftware/calls-blocker
PhoneNumber library
手机号段归属地数据库
https://github.com/zengzhan/qqzeng-ip?files=1
blog:https://www.cnblogs.com/zengxiangzhan/p/phone.html
https://www.qqzeng.com/article/dev.html
PhoneNumber library
come from:https://github.com/jieeit/phone
php:解析phone.dat
