为来自我自己的应用程序的来电提供来电显示 [英] Provide the caller id for incoming call from my own app

查看:40
本文介绍了为来自我自己的应用程序的来电提供来电显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序,通过查看我的应用程序内的表(例如 SQLite 数据库表)来识别传入(未知)呼叫的电话号码.

I want to write an App that can identify the phone number of incoming (unknown) calls by looking into a table inside my app (e.g. an SQLite Database Table).

我已经使用 Call Directory Extension 在 iOS 中实现了这一点,但对于 Android,我能找到的唯一选择是使用BroadcastReceiver 上方有一个弹出窗口本机来电屏幕显示联系信息.

I already implemented this in iOS using a Call Directory Extension, but for Android the only option I can find is using a BroadcastReceiver with a popup above the native incoming call screen to show contact information.

ContactsContract.Directory 似乎提供了创建的可能性本机呼叫应用程序可以在其中查找呼叫者 ID 的自定义目录.不幸的是,我找不到任何关于如何实现这一点的好例子.

ContactsContract.Directory seems to offer the possibility to create custom directories where the native call app could look up the caller id. Unfortunately, I can't find any good examples on how to accomplish this.

有人知道是否可以使用 ContactsContract.Directory 来实现来电显示,或者 Android 中是否有与 Android 中的呼叫目录扩展类似的东西?如果是这种情况,示例代码将非常有用.

Does anybody know if it is possible to implement the caller id using the ContactsContract.Directory or if there is something similar in Android to the Call Directory Extension in Android? If that is the case, an example code would be really helpful.

推荐答案

我刚刚写了一篇关于如何让它工作的教程.在这里查看:https://simplenexus.dev/2019/08/27/android-caller-id.html

I just barely wrote a tutorial on how to get this working. Check it out here: https://simplenexus.dev/2019/08/27/android-caller-id.html

如何使其工作的基础知识是:

The basics of how to get this working are:

AndroidManifest.xml

AndroidManifest.xml

<provider
android:name=".callerid.CallerIDProvider"
android:authorities="@string/callerid_authority"
android:readPermission="android.permission.READ_CONTACTS"
android:enabled="true"
android:exported="true">
<meta-data
  android:name="android.content.ContactDirectory"
  android:value="true"/></provider>

CallerIDProvider.kt

CallerIDProvider.kt

private var userRepository: UserRepository? = null

private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)

override fun onCreate(): Boolean {
    context?.let {
        val userDao = UserDatabase.getDatabase(it).userDao()
        userRepository = UserRepository(userDao)
        val authority = it.getString(R.string.callerid_authority)
        uriMatcher.addURI(authority, "directories", DIRECTORIES)
        uriMatcher.addURI(authority, "phone_lookup/*", PHONE_LOOKUP)
    }
    return true
}

override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
    when (uriMatcher.match(uri)) {
        DIRECTORIES -> {
            val label = context?.getString(R.string.app_name) ?: return null
            val cursor = MatrixCursor(projection)
            projection?.map { column ->
                when (column) {
                    Directory.ACCOUNT_NAME,
                    Directory.ACCOUNT_TYPE,
                    Directory.DISPLAY_NAME -> label
                    Directory.TYPE_RESOURCE_ID -> R.string.app_name
                    Directory.EXPORT_SUPPORT -> Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY
                    Directory.SHORTCUT_SUPPORT -> Directory.SHORTCUT_SUPPORT_NONE
                    else -> null
                }
            }?.let { cursor.addRow(it) }
            return cursor
        }
        PHONE_LOOKUP -> {
            userRepository?.let { userRepo ->
                val phoneNumber = uri.pathSegments[1]
                val cursor = MatrixCursor(projection)
                val user = runBlocking(Dispatchers.IO) { userRepo.getUser(phoneNumber) }
                user?.let { u ->
                    projection?.map { column ->
                        when (column) {
                            PhoneLookup._ID -> -1
                            PhoneLookup.DISPLAY_NAME -> u.fullName
                            PhoneLookup.LABEL -> u.phoneLabel
                            else -> null
                        }
                    }?.let { cursor.addRow(it) }
                }
                return cursor
            }
        }
    }
    return null
}

这篇关于为来自我自己的应用程序的来电提供来电显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆