android - set custom ringtone to specific contact

2019-02-19 13:01发布

问题:

I am trying to develop android app, I need to assign ringtone to specific contact number without permit the user to access list of contact.

here is the code for assigning ringtone to all contact :

File k = new File("/sdcard/AudioRecorder", "hello.mp4");

   // Uri i = data.getData(); //getDATA
    //String s = i.getPath(); //getPath
   // File k = new File(s); //set File from path

//if(s!=null){  //(file.exists

    ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "ring");
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp4");
       values.put(MediaStore.MediaColumns.SIZE, k.length());
       values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
       values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
       values.put(MediaStore.Audio.Media.IS_ALARM, true);
       values.put(MediaStore.Audio.Media.IS_MUSIC, false);

       Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
       getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
    Uri newUri = getContentResolver().insert(uri, values);
try {
           RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
       } catch (Throwable t) {

       }   

How can I do this for specific contact?

回答1:

set custom ringtone to specific contact number

Android has a special column for this: ContactsContract.CUSTOM_RINGTONE.

So, you could use ContactsContract.Contacts.getLookupUri to get your contact's Uri, after that pretty much all that's left is to call ContentResolver.update.

Here's an example of looking up a contact by their phone number, then applying a custom ringtone:

import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;

// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
// The columns used for `Contacts.getLookupUri`
final String[] projection = new String[] {
        Contacts._ID, Contacts.LOOKUP_KEY
};
// Build your Cursor
final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
data.moveToFirst();
try {
    // Get the contact lookup Uri
    final long contactId = data.getLong(0);
    final String lookupKey = data.getString(1);
    final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
    if (contactUri == null) {
        // Invalid arguments
        return;
    }

    // Get the path of ringtone you'd like to use
    final String storage = Environment.getExternalStorageDirectory().getPath();
    final File file = new File(storage + "/AudioRecorder", "hello.mp4");
    final String value = Uri.fromFile(file).toString();

    // Apply the custom ringtone
    final ContentValues values = new ContentValues(1);
    values.put(Contacts.CUSTOM_RINGTONE, value);
    getContentResolver().update(contactUri, values, null, null);
} finally {
    // Don't forget to close your Cursor
    data.close();
}

Also, you'll need to add both permissions to read and write contacts:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />