【问题标题】:android - set custom ringtone to specific contact numberandroid - 将自定义铃声设置为特定的联系人号码
【发布时间】:2014-05-01 08:11:28
【问题描述】:

我正在尝试开发 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) {

       }   

我该如何为特定联系人执行此操作?

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

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

    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" />
    

    【讨论】:

    • 我正在尝试使用您的代码更改铃声。我获取相关联系人,我正在将一个 ogg 文件设置为铃声(在设置之前,我正在检查文件是否存在以防万一:file.exists())一切看起来都很好,没有任何错误,但联系人的铃声没有不改变。你有什么建议或线索吗,因为我已经为此工作了 3 天:(
    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2017-05-27
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多