如何使用ringtoneType all指定RingtonePreference的默认铃声

时间:2015-09-11 18:05:19

标签: android-preferences

我试图设置一个带有ringtoneType all的RingtonePreference,但是我试图将默认声音设为默认通知声音。现在,它默认为默认铃声......我该如何改变? (WhatsApp能够做到这一点,所以我知道它可能)

1 个答案:

答案 0 :(得分:0)

结束只是创建一个RingtonePreference的子类,覆盖onPrepareRingtonePickerIntent方法以始终向RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI提供默认通知音,这是非常小的改变,所以希望它是安全的。这是我的班级:

package com.myexample;

import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.preference.RingtonePreference;
import android.util.AttributeSet;

public class MyRingtonePreference extends RingtonePreference {
    public MyRingtonePreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyRingtonePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRingtonePreference(Context context) {
        super(context);
    }

    protected void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) {

        ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                onRestoreRingtone());

        ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, getShowDefault());
        if (getShowDefault()) {
            ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
                    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }

        ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, getShowSilent());
        ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, getRingtoneType());
        ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getTitle());
    }
}