首页 > 代码库 > android如何替换contact的来电铃声

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


android如何替换contact的来电铃声