get Long value from Bundle by converting Integer and String value to long - Android android.os

Android examples for android.os:Bundle

Description

get Long value from Bundle by converting Integer and String value to long

Demo Code

import android.os.Bundle;

public class Main {

  /**/* w  ww  .  j  a  v  a  2  s  . c o m*/
   * coerce value to a long, no matter what type of object is in the bundle
   * 
   * @param bundle
   * @param key
   * @param defaultValue
   * @return
   */
  public static long getLong(Bundle bundle, String key, long defaultValue) {
    Object value = bundle.get(key);
    if (value instanceof Long) {
      return (Long) value;
    } else if (value instanceof Integer) {
      return ((Integer) value).longValue();
    } else if (value instanceof String) {
      try {
        return Long.valueOf((String) value);
      } catch (NumberFormatException e) {
        // value is not a long
      }
    }
    return defaultValue;
  }

}

Related Tutorials