Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

java - Is there a way to asign a custom ringtone to a specific contact on Android 10?

The question says it all, i have a function to which i pass a phone number and the function asigns a predownloaded ringtone to a contact with the passed phone number (if such a contact exists). The problem is that it only works on Android 9 and below, when i run it on an Android 10 it just does nothing, no exceptions, no crashes, nothing..

Heres the functions if anybody knows whats up:

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,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, null)
        Log.i(TAG_LABEL, "content resolver res: " + res.toString())

    }finally {
        data.close();
    }

    return "succes"
}

Once again, it runs fine on Android 9 and below. Also, i can't seem to find any information in the official documentation that mentions changes in Contacts services or similar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...