【问题标题】:Provide the caller id for incoming call from my own app为来自我自己的应用程序的来电提供来电显示
【发布时间】:2018-07-29 23:03:16
【问题描述】:

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

我已经在 iOS 中使用 Call Directory Extension 实现了这一点,但对于 Android,我能找到的唯一选择是使用 BroadcastReceiver with a popup above the native incoming call screen 显示联系信息。

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

有人知道是否可以使用 ContactsContract.Directory 来实现呼叫者 ID,或者在 Android 中是否有与 Android 中的呼叫目录扩展类似的东西?如果是这样的话,一个示例代码将非常有帮助。

【问题讨论】:

    标签: android ios android-contentprovider contacts ios-extensions


    【解决方案1】:

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

    如何让这个工作的基础是:

    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

    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
    }
    

    【讨论】:

    • 博客有更多信息/上下文。值得一试
    • 默认的三星手机应用不检查其他应用中的android.content.ContactDirectory。您可能可以下载一个不同的手机应用程序来检查,在这种情况下,这对三星有效。
    • @Jacob Michaelis 我在三星 S9 上下载了另一个手机应用程序,但不幸的是我无法从我自己的应用程序中获取来电显示。在摩托罗拉上,它确实运行良好。您能否提供更多信息如何使三星手机也能做到这一点?也许您已经找到了一些解决方法?您在三星手机上安装了哪个手机应用程序才能使其正常工作?你之前必须root手机吗?提前谢谢了。干杯。
    • 我实际上还没有找到另一个手机应用程序来检查三星的正确意图。我想在我这样做之前,我测试它是否适用于三星,我无法确认问题不是三星操作系统,而是应用程序。我的推测是基于对 Android Intents 如何工作的了解,所以我假设应用程序只需要寻找正确的 Intent 来使用 Caller ID 信息。有兴趣看看是否有人解决了这个问题。
    • 我遇到了同样的问题。你们有三星设备的解决方案吗?这不适用于默认的三星拨号器。
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 2021-04-27
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多