2015-06-27

[Android] 從鈴聲的URI取得鈴聲的標題名稱. Get ringtone title from ringtone uri




String getRingtoneTitle(String ringtoneUriString) {
    
    String defaultRingtoneTitle = "No Ringtone";
    Uri ringtoneUri = null;
    try {
        ringtoneUri = Uri.parse(ringtoneUriString);
    } catch (Exception ignored) {
        Logcat.w(ignored, "Settings parse notification ringtone uri fail."
            + "ringtoneUriString = ", ringtoneUriString);
    }
    if (ringtoneUriString.isEmpty() || ringtoneUri == null) {
        return defaultRingtoneTitle;
    }
    Context context = getContext();
    Ringtone ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
    if (ringtone == null) {
        return defaultRingtoneTitle;
    }
    return ringtone.getTitle(context);
}

[Android] 如何設定"提醒"的聲音、閃光、震動形式. How to set the Sound, Lights, and Vibrate of a Notification

If you don't want any ringtone, don't set Notification.DEFAULT_SOUND to setDefault(), and don't call setSound(ringtoneUri) of NotificationCompat.Builder.

 If you just want default ringtone, call setDefault(Notification.DEFAULT_SOUND);

 If you just want default vibrate, call setDefault(Notification.DEFAULT_VIBRATE);

 If you just want default lights, call setDefault(Notification.DEFAULT_LIGHTS);

If you need a custom ringtone, call setSound(ringtoneUri), but don't call setDefault(DEFAULT_ALL) or setDefault(DEFAULT_SOUND).
Because setDefault(DEFAULT_ALL) or setDefault(DEFAULT_SOUND) make setSound(ringtoneUri) useless.

Check here about how to use Ringtone Picker to get a ringtone Uri.
http://codeviki.blogspot.tw/2015/06/android-show-ringtone-picker-activity.html

Check here about how to get ringtone title from ringtone Uri.
http://codeviki.blogspot.tw/2015/06/android-get-ringtone-title-from-ringtone-uri.html


static Notification buildNotification(
 boolean vibrateEnabled,
 boolean lightsEnabled,
 String ringtoneUriString)
{
 int defaults = 0;

 // set vibrate to defaults
 if (vibrateEnabled) {
  // use default vibrate pattern
     defaults |= Notification.DEFAULT_VIBRATE;
 }

 // set lights to defaults
 if (lightsEnabled) {
     // use default light blink pattern
     defaults |= Notification.DEFAULT_LIGHTS;
 }

 // parse ringtone
 Uri ringtoneUri = null;
    try {
        ringtoneUri = Uri.parse(ringtoneUriString);
    } catch (Exception ignored) {
        Logcat.w(ignored, "Parse notification ringtone uri fail. "
            + "ringtoneUriString = ", ringtoneUriString);
        // if parse ringtone fail, use default ringtone
        defaults |= Notification.DEFAULT_SOUND;
    }
 
 NotificationCompat.Builder builder =
     new NotificationCompat.Builder(context)
         .setSmallIcon(R.drawable.my_notification_icon)
         .setContentTitle("title")
         .setContentText("contentText")
         .setAutoCancel(true)
         .setDefaults(defaults);

 // if ringtoneUriString is empty string, the uri won't be null
    if (!ringtoneUriString.isEmpty() && ringtoneUri != null) {
     builder.setSound(ringtoneUri);
 }

 return builder.build();
}

[Android] 顯示讓使用者選擇鈴聲的對話方塊. Show Ringtone Picker Activity

// just define a unique int for this ringtone picker activity
public static final int RINGTONE_PICKER_REQUEST = 10;

void showRingtonePicker() {
    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
    Activity activity =getActivity();
    if (activity == null) {
        return;
    }
    try {
        activity.startActivityForResult(intent, RINGTONE_PICKER_REQUEST);
    } catch (ActivityNotFoundException ignored) {
        Logcat.e(ignored, "Settings showRingtonePicker error");
    }
}
To get the result of ringtone picker, override onActivityResult in Activity.
@Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode != RINGTONE_PICKER_REQUEST || resultCode != Activity.RESULT_OK) {
        return false;
    }
    Uri ringtoneUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    // if user choose "no ringtone" in the ringtone picker, the ringtoneUri would be null.

    String ringtoneUriString = "";
    if (uri != null) {
        ringtoneUriString = uri.toString();
    }
    
    // TODO : save the ringtoneUri or ringtoneUriString to anywhere you want
    
    return true;
}