微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在首选项中,选择我的声音就像铃声优先

我的/ raw文件夹中有声音,我希望我的用户能够选择一个声音,就像“铃声优先”一样,但只有我的声音.

解决方法

这里我的铃声优先替换.
列出所有系统铃声和您的自定义铃声(以xml定义,存储在res / raw中)

ExtraringtonePreference.java

package de.almisoft.test;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import de.almisoft.test.R;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.media.ringtone;
import android.media.ringtoneManager;
import android.net.Uri;
import android.preference.DialogPreference;
import android.util.AttributeSet;

public class ExtraringtonePreference extends DialogPreference {

    private Context mContext;
    private String mValue;
    private ringtone ringtone;
    private int mringtoneType;
    private boolean mShowSilent;
    private boolean mShowDefault;
    private CharSequence[] mExtraringtones;
    private CharSequence[] mExtraringtoneTitles;

    public ExtraringtonePreference(Context context,AttributeSet attrs) {

        super(context,attrs);

        mContext = context;

        final TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ExtraringtonePreference,0);

        mringtoneType = a.getInt(R.styleable.ExtraringtonePreference_ringtoneType,ringtoneManager.TYPE_ringtone);
        mShowDefault = a.getBoolean(R.styleable.ExtraringtonePreference_showDefault,true);
        mShowSilent = a.getBoolean(R.styleable.ExtraringtonePreference_showSilent,true);
        mExtraringtones = a.getTextArray(R.styleable.ExtraringtonePreference_extraringtones);
        mExtraringtoneTitles = a.getTextArray(R.styleable.ExtraringtonePreference_extraringtoneTitles);

        a.recycle();
    }

    public ExtraringtonePreference(Context context) {
        this(context,null);
    }

    public String getValue() {
        return mValue;
    }

    private Map<String,Uri> getSounds(int type) {

        ringtoneManager ringtoneManager = new ringtoneManager(mContext);
        ringtoneManager.setType(type);
        Cursor cursor = ringtoneManager.getCursor();

        Map<String,Uri> list = new TreeMap<String,Uri>();
        while (cursor.movetoNext()) {
            String notificationTitle = cursor.getString(ringtoneManager.TITLE_COLUMN_INDEX);
            Uri notificationUri =  ringtoneManager.getringtoneUri(cursor.getPosition());

            list.put(notificationTitle,notificationUri);
        }

        return list;
    }

    private Uri uriFromraw(String name) {
        int resId = mContext.getResources().getIdentifier(name,"raw",mContext.getPackageName());
        return Uri.parse("android.resource://" + mContext.getPackageName() + "/" + resId);
    }

    private String getExtraringtoneTitle(CharSequence name) {
        if (mExtraringtones != null && mExtraringtoneTitles != null) {
            int index = Arrays.asList(mExtraringtones).indexOf(name);
            return mExtraringtoneTitles[index].toString(); 
        } 

        return null;
    }

    @Override
    public CharSequence getSummary() {

        String ringtoneTitle = null;

        if (mValue != null) {

            if (mValue.length() == 0)
                ringtoneTitle = mContext.getString(R.string.silent);

            if (ringtoneTitle == null && mExtraringtones != null && mExtraringtoneTitles != null) {

                for (int i = 0; i < mExtraringtones.length; i++) {

                    Uri uriExtra = uriFromraw(mExtraringtones[i].toString());

                    if (uriExtra.equals(Uri.parse(mValue))) {
                        ringtoneTitle = mExtraringtoneTitles[i].toString();
                        break;
                    }
                }
            }

            if (ringtoneTitle == null) {
                ringtone ringtone = ringtoneManager.getringtone(mContext,Uri.parse(mValue));
                String title = ringtone.getTitle(mContext);
                if (title != null && title.length() > 0)
                ringtoneTitle = title;
            }

        }

        CharSequence summary = super.getSummary();

        if (ringtoneTitle != null) { 
            if (summary != null)
                return String.format(summary.toString(),ringtoneTitle);
            else
                return ringtoneTitle;
        } else return summary;
}

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {

        final Map<String,Uri> sounds = new LinkedHashMap<String,Uri>();

        if (mExtraringtones != null) {
            for (CharSequence extraringtone : mExtraringtones) {
                Uri uri = uriFromraw(extraringtone.toString());
                String title = getExtraringtoneTitle(extraringtone);

                sounds.put(title,uri);
            }
        }

        if (mShowDefault) {
            Uri uriDefault = ringtoneManager.getDefaultUri(mringtoneType);
            if (uriDefault != null) {
                ringtone ringtoneDefault = ringtoneManager.getringtone(mContext,uriDefault);
                if (ringtoneDefault != null) {
                    sounds.put(ringtoneDefault.getTitle(mContext),uriDefault);
                }
            }   
        }

        if (mShowSilent) 
            sounds.put(mContext.getString(R.string.silent),Uri.parse(""));


        sounds.putAll(getSounds(ringtoneManager.TYPE_NOTIFICATION));


        final String[] titleArray = sounds.keySet().toArray(new String[0]);
        final Uri[] uriArray = sounds.values().toArray(new Uri[0]);

        int index = mValue != null ? Arrays.asList(uriArray).indexOf(Uri.parse(mValue)) : -1;

        builder.setSingleChoiceItems(titleArray,index,new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,int which) {

                if (ringtone != null)
                    ringtone.stop();

                String title = titleArray[which];
                Uri uri = uriArray[which];

                if (uri != null) {
                    if (uri.toString().length() > 0) {
                        ringtone = ringtoneManager.getringtone(mContext,uri);
                        ringtone.play();
                    }
                    mValue = uri.toString();
                } else mValue = null;

            }
        });

        builder.setPositiveButton(R.string.ok,this);
        builder.setNegativeButton(R.string.cancel,this);

    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {

        super.onDialogClosed(positiveResult);

        if (ringtone != null)
            ringtone.stop();

        if (positiveResult && callchangelistener(mValue)) {
            persistString(mValue);
            notifyChanged();
        }

    }

    @Override
    protected Object onGetDefaultValue(TypedArray a,int index) {
        return a.getString(index);
    }

    @Override
    protected void onSetinitialValue(boolean restoreValue,Object defaultValue) {

        if (restoreValue)
            mValue = getPersistedString("");
        else {
            if (mExtraringtones != null && defaultValue != null && defaultValue.toString().length() > 0) {

                int index = Arrays.asList(mExtraringtones).indexOf((CharSequence) defaultValue);
                if (index >= 0) 
                    mValue = uriFromraw(defaultValue.toString()).toString();
                else mValue = (String) defaultValue; 

        } else mValue = (String) defaultValue; 

            persistString(mValue);
        }


    }


}

RES /值/ attrs.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="ExtraringtonePreference">
        <attr name="ringtoneType">
            <!-- ringtones. -->
            <flag name="ringtone" value="1" />
            <!-- Notification sounds. -->
            <flag name="notification" value="2" />
            <!-- Alarm sounds. -->
            <flag name="alarm" value="4" />
            <!-- All available ringtone sounds. -->
            <flag name="all" value="7" />
        </attr>
        <attr name="showSilent" format="boolean"/>
        <attr name="showDefault" format="boolean"/>
        <attr name="extraringtones" format="reference"/>
        <attr name="extraringtoneTitles" format="reference"/>
    </declare-styleable>

</resources>

RES /值/ strings.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="silent">Silent</string>
    <string name="ok">OK</string>
    <string name="cancel">Cancel</string>
    <string name="ringtoneTitle">ringtone</string>
    <string name="ringtoneSummary">ringtone: %s</string>

    <string-array name="extraringtones">
        <item>deichkind_sone_musik</item>
        <item>madonna_like_a_virgin</item>
    </string-array>

    <string-array name="extraringtoneTitles">
        <item>Sone Musik</item>
        <item>Like A Virgin</item>
    </string-array>
</resources>

RES /生

res
 ↳ raw
    ↳ deichkind_sone_musik.mp3
    ↳ madonna_like_a_virgin.mp3

RES / XML /的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:auto="http://schemas.android.com/apk/res-auto">

    <de.almisoft.test.ExtraringtonePreference
        android:key="ringtone"
        android:title="@string/ringtoneTitle"
        android:summary="@string/ringtoneSummary"
        android:defaultValue="deichkind_sone_musik"
        auto:ringtoneType="notification"
        auto:showSilent="true"
        auto:showDefault="true"
        auto:extraringtones="@array/extraringtones"
        auto:extraringtoneTitles="@array/extraringtoneTitles"/>

    <!-- set android:defaultValue 
         to "deichkind_sone_musik" for your custom mp3
         to "" for silent
         to "content://settings/system/notification_sound" for system default ringtone -->

</PreferenceScreen>

原文地址:https://www.jb51.cc/android/310494.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐