BundleUtil

本文详细介绍了BundleUtil工具类的功能与使用方法,包括如何获取Bundle中的字符串、布尔值、整数、浮点数等不同类型的数据,并展示了如何通过自定义泛型方法灵活地获取任意类型的数据。此外,还提供了Bundle内容展示的示例,帮助开发者更好地理解并应用BundleUtil在实际项目中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 *
 * Copyright (c) 2014 CoderKiss
 *
 * CoderKiss[AT]gmail.com
 *
 */

package com.kisstools.utils;

import java.util.Set;

import android.os.Bundle;
import android.text.TextUtils;

public class BundleUtil {
   public static final String TAG = "BundleUtil";

   public static String getString(Bundle bundle, String key) {
      return getString(bundle, key, "");
   }

   private static String getString(Bundle bundle, String key, String df) {
      if (df == null) {
         df = "";
      }
      return getValue(bundle, key, df);
   }

   public static boolean getBoolean(Bundle bundle, String key, boolean df) {
      return getValue(bundle, key, df);
   }

   public static int getInt(Bundle bundle, String key) {
      return getInt(bundle, key, 0);
   }

   public static int getInt(Bundle bundle, String key, int df) {
      return getValue(bundle, key, df);
   }

   public static double getDouble(Bundle bundle, String key) {
      return getDouble(bundle, key, 0.0);
   }

   public static double getDouble(Bundle bundle, String key, double df) {
      return getValue(bundle, key, df);
   }

   @SuppressWarnings("unchecked")
   public static <T> T getValue(Bundle bundle, String key, T df) {
      if (bundle == null || TextUtils.isEmpty(key)) {
         return df;
      }

      if (df == null) {
         return df;
      }

      if (!bundle.containsKey(key)) {
         return df;
      }
      T value = df;
      Object obj = bundle.get(key);
      if (obj != null && value.getClass().isAssignableFrom(obj.getClass())) {
         value = (T) obj;
      } else {
         LogUtil.w(TAG, "[key] " + key + " [value] " + obj);
      }
      return value;
   }

   public static final void showContent(Bundle bundle) {
      if (bundle == null) {
         return;
      }

      StringBuilder builder = new StringBuilder();
      builder.append("bundle contnet\n");
      Set<String> keys = bundle.keySet();
      for (String key : keys) {
         builder.append("[" + key + "]<->[" + bundle.get(key) + "]\n");
      }
      String content = builder.toString();
      LogUtil.d(TAG, content);
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值