有没有办法为 Android 10 上的特定联系人分配自定义铃声?

问题描述

问题说明了一切,我有一个函数可以将电话号码传递给该函数,并且该函数会将预下载的铃声分配给具有传递电话号码的联系人(如果存在此类联系人)。问题是它只适用于 Android 9 及更低版本,当我在 Android 10 上运行它时,它什么也不做,没有异常,没有崩溃,什么都没有..

如果有人知道怎么回事,这里是函数

private fun setCustomringtone(targetContactPhone: String): String {

    val lookupUri = Uri.withAppendedpath(PhoneLookup.CONTENT_FILTER_URI,targetContactPhone)
    val projection = arrayOf<String>(Contacts._ID,Contacts.LOOKUP_KEY)
    
    val data = contentResolver.query(lookupUri,projection,null,null)!!
    data.movetoFirst()

    try {
        // Get the contact lookup Uri
        val contactID: Long = data.getLong(0)
        val lookupKey = data.getString(1);
    
        val contactUri = Contacts.getLookupUri(contactID,lookupKey)
        if (contactUri == null){
            Log.i(TAG_LABEL,"contactUri je null,invalid arguments?")
            return "fail";
        }
        Log.i(TAG_LABEL,"contact uri: " + contactUri.toString()) // valid and expected Uri

        // Get the path of the ringtone you'd like to use
        val storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path
        Log.i(TAG_LABEL,"out: $storage")
        
        val file = File(storage,"ringtone.mp3")
        Log.i(TAG_LABEL,file.exists().toString()) // reasures me that the file exists
        
        val value = Uri.fromFile(file).toString()
        
        Log.i(TAG_LABEL,"size: " + file.length().toString() + " name: " + file.name)

        val values = ContentValues()
        values.put(Contacts.CUSTOM_ringtone,value)

        // TRIED A COMBINATION OF THESE,They DON'T WORK
        // values.put(MediaStore.MediaColumns.TITLE,file.nameWithoutExtension);
        // values.put(MediaStore.MediaColumns.MIME_TYPE,"audio/mpeg");
        // values.put(MediaStore.MediaColumns.SIZE,file.length())
        // values.put(MediaStore.Audio.Media.IS_ringtone,true)
        // values.put(MediaStore.Audio.Media.IS_NOTIFICATION,false);
        // values.put(MediaStore.Audio.Media.IS_ALARM,false);
        // values.put(MediaStore.Audio.Media.IS_MUSIC,false);
        // values.put(MediaStore.Audio.Media.ARTIST,"example")

        val res = contentResolver.update(contactUri,values,null)
        Log.i(TAG_LABEL,"content resolver res: " + res.toString())

    }finally {
        data.close();
    }

    return "succes"
}

再一次,它在 Android 9 及更低版本上运行良好。此外,我似乎无法在官方文档中找到任何提及通讯录服务或类似更改的信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)