android - 将自定义铃声设置为特定的联系号码

时间:2014-05-01 08:11:29

标签: android android-contacts

我正在尝试开发Android应用程序,我需要将铃声分配给特定的联系号码,而不允许用户访问联系人列表。

这是为所有联系人分配铃声的代码:

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) {

       }   

如何进行特定联系?

1 个答案:

答案 0 :(得分:4)

  

将自定义铃声设置为特定联系号码

Android有一个专栏:ContactsContract.CUSTOM_RINGTONE

因此,您可以使用ContactsContract.Contacts.getLookupUri来获取联系人Uri,之后剩下的就是致电ContentResolver.update

以下是通过电话号码查找联系人,然后应用自定义铃声的示例:

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();
}

此外,您还需要添加读取和写入联系人的权限:

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