1

在我的应用程序中,有一个更改铃声的选项。铃声选择器弹出,用户可以更改它。因此,当重新启动平板电脑时,铃声已设置但未在选择器上标记。我怎样才能做到这一点 ?在其他作品中,铃声选择器显示没有正确设置。

private void setRington()
{
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    if (notification_uri == null)
    {
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
    }
    else
    {
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(notification_uri));
    }
    startActivityForResult(intent, 5);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == Activity.RESULT_OK && requestCode == 5)
    {
        Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
         if (uri != null)
        {
            RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);
            Settings.System.putString(getContentResolver(), Settings.System.NOTIFICATION_SOUND, uri.toString());
            notification_uri = uri.toString();
        }
    }
}
4

1 回答 1

0

您必须将所选铃声存储到应用首选项中并使用它来设置现有铃声。

public static void storeToneUri(Context context,
        Uri toneUri) {
    if(toneUri == null)
        return;

    Editor editor = context.getSharedPreferences(PREFERENCES_NAME,
            Context.MODE_PRIVATE).edit();
    editor.putString(PROPERTY_NOTIFICATION_SOUND, toneUri.toString());
    editor.commit();
}

public static Uri getToneUri(Context context) {

    SharedPreferences prefs = context.getSharedPreferences(
            PREFERENCES_NAME, Context.MODE_PRIVATE);

    Uri toneUri = Settings.System.DEFAULT_NOTIFICATION_URI;
    String soundString = prefs.getString(PROPERTY_NOTIFICATION_SOUND, null);
    if(soundString != null) {
        toneUri = Uri.parse(soundString);
        if(toneUri == null) {
            toneUri = Settings.System.DEFAULT_NOTIFICATION_URI;
        }
    }

    return toneUri;
} 
于 2014-06-05T13:55:51.420 回答