android 15,写一个防诈骗电话apk,源代码
开发一个针对 Android 15 (API Level 35) 的防诈骗电话拦截 App,需要通过注册 CallScreeningService 来在系统层面拦截并挂断电话。
Table of Contents
tools:Android Studio Rabbit 1 | 2026.2.1
lan: java
开发
step 1.在 Android Studio 中新建一个 Empty Views Activity (Kotlin) 项目
Empty Views Activity:传统的 java 开发模式。
step 2.
x:\x\AntiScamBlocker\app\build\outputs\apk\debug
step 2.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());
}
}
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.
1. AndroidManifest.xml Configuration - register the 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>
2. 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);
}
}
}
在 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
