Saring panggilan

Perangkat yang menjalankan Android 10 (API level 29) atau yang lebih baru memungkinkan aplikasi Anda mengidentifikasi panggilan dari nomor yang tidak ada di buku alamat pengguna sebagai potensi panggilan spam. Pengguna dapat memilih untuk menolak panggilan spam secara diam-diam. Untuk memberikan transparansi yang lebih besar kepada pengguna saat mereka melewatkan panggilan, informasi tentang panggilan yang diblokir ini dicatat ke dalam log panggilan. Penggunaan Android 10 API menghilangkan persyaratan untuk mendapatkan izin READ_CALL_LOG dari pengguna untuk menyediakan fungsi penyaringan panggilan dan ID penelepon.

Anda menggunakan implementasi CallScreeningService untuk menyaring panggilan. Panggil fungsi onScreenCall() untuk semua panggilan masuk atau keluar baru saat nomor tersebut tidak ada dalam daftar kontak pengguna. Anda dapat memeriksa objek Call.Details untuk mengetahui informasi tentang panggilan. Secara khusus, fungsi getCallerNumberVerificationStatus() menyertakan informasi dari penyedia jaringan tentang angka lainnya. Jika status verifikasi gagal, ini adalah indikasi yang baik bahwa panggilan berasal dari nomor yang tidak valid atau kemungkinan panggilan spam.

Kotlin

class ScreeningService : CallScreeningService() {
    // This function is called when an ingoing or outgoing call
    // is from a number not in the user's contacts list
    override fun onScreenCall(callDetails: Call.Details) {
        // Can check the direction of the call
        val isIncoming = callDetails.callDirection == Call.Details.DIRECTION_INCOMING

        if (isIncoming) {
            // the handle (e.g. phone number) that the Call is currently connected to
            val handle: Uri = callDetails.handle

            // determine if you want to allow or reject the call
            when (callDetails.callerNumberVerificationStatus) {
                Connection.VERIFICATION_STATUS_FAILED -> {
                    // Network verification failed, likely an invalid/spam call.
                }
                Connection.VERIFICATION_STATUS_PASSED -> {
                    // Network verification passed, likely a valid call.
                }
                else -> {
                    // Network could not perform verification.
                    // This branch matches Connection.VERIFICATION_STATUS_NOT_VERIFIED.
                }
            }
        }
    }
}

Java

class ScreeningService extends CallScreeningService {
    @Override
    public void onScreenCall(@NonNull Call.Details callDetails) {
        boolean isIncoming = callDetails.getCallDirection() == Call.Details.DIRECTION_INCOMING;

        if (isIncoming) {
            Uri handle = callDetails.getHandle();

            switch (callDetails.getCallerNumberVerificationStatus()) {
                case Connection.VERIFICATION_STATUS_FAILED:
                    // Network verification failed, likely an invalid/spam call.
                    break;
                case Connection.VERIFICATION_STATUS_PASSED:
                    // Network verification passed, likely a valid call.
                    break;
                default:
                    // Network could not perform verification.
                    // This branch matches Connection.VERIFICATION_STATUS_NOT_VERIFIED
            }
        }
    }
}

Tetapkan fungsi onScreenCall() untuk memanggil respondToCall() guna memberi tahu sistem cara merespons panggilan baru. Fungsi ini menggunakan parameter CallResponse yang dapat Anda gunakan untuk memberi tahu sistem agar memblokir panggilan, menolak seolah-olah pengguna melakukannya, atau membisukannya. Anda juga dapat memberi tahu sistem untuk melewati penambahan panggilan ini ke log panggilan perangkat sepenuhnya.

Kotlin

// Tell the system how to respond to the incoming call
// and if it should notify the user of the call.
val response = CallResponse.Builder()
    // Sets whether the incoming call should be blocked.
    .setDisallowCall(false)
    // Sets whether the incoming call should be rejected as if the user did so manually.
    .setRejectCall(false)
    // Sets whether ringing should be silenced for the incoming call.
    .setSilenceCall(false)
    // Sets whether the incoming call should not be displayed in the call log.
    .setSkipCallLog(false)
    // Sets whether a missed call notification should not be shown for the incoming call.
    .setSkipNotification(false)
    .build()

// Call this function to provide your screening response.
respondToCall(callDetails, response)

Java

// Tell the system how to respond to the incoming call
// and if it should notify the user of the call.
CallResponse.Builder response = new CallResponse.Builder();
// Sets whether the incoming call should be blocked.
response.setDisallowCall(false);
// Sets whether the incoming call should be rejected as if the user did so manually.
response.setRejectCall(false);
// Sets whether ringing should be silenced for the incoming call.
response.setSilenceCall(false);
// Sets whether the incoming call should not be displayed in the call log.
response.setSkipCallLog(false);
// Sets whether a missed call notification should not be shown for the incoming call.
response.setSkipNotification(false);

// Call this function to provide your screening response.
respondToCall(callDetails, response.build());

Anda harus mendaftarkan implementasi CallScreeningService dalam file manifes dengan filter intent dan izin yang sesuai agar sistem dapat memicu dengan benar.

<service
    android:name=".ScreeningService"
    android:permission="android.permission.BIND_SCREENING_SERVICE">
    <intent-filter>
        <action android:name="android.telecom.CallScreeningService" />
    </intent-filter>
</service>