android如何替换contact的来电铃声

如果知道contactId,那么可以这样获取已有的ringtone:

 public String getContactRington(long contactId) {
        String rington = "";
        String[] projection = { ContactsContract.Contacts.CUSTOM_RINGTONE };
        Uri contactUri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, contactId);
        Cursor cursor = this.getContentResolver().query(contactUri,
                projection, null, null, null);
        if (cursor == null) {
            return rington;
        }
        while (cursor.moveToNext()) {
            rington = cursor.getString(0);
            break;
        }

        cursor.close();
        return rington;
    }

用以下代码可以去调用系统接口改变铃声:

 private void doPickRingtone(String ringtones) {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);

        try {
            Uri ringtoneUri;
            if (ringtones != null
                    && ringtones.length() > 0) {
                ringtoneUri = Uri.parse(ringtones);
            } else {
                ringtoneUri = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                    ringtoneUri);
            startActivityForResult(intent, RESUTL_RINGTONE_PICKED);
        } catch (Exception ex) {
            MktLog.i(TAG,"doPickRingtone:" + ex.toString());
        }
    }

选择铃声完点击确认:

  private void handleRingtonePicked(Uri pickedUri) {
        updateContactRington(mContactId,pickedUri.toString());
    }

    public boolean updateContactRington(long contactId, String rington) {
        ContentValues values = new ContentValues();
        values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, rington);
        Uri contactUri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI, contactId);
        this.getContentResolver().update(contactUri, values, null, null);
        return true;
    }


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。