|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- package com.hjq.demo.helper;
-
- import android.app.Activity;
- import android.app.Fragment;
- import android.content.Context;
- import android.content.Intent;
-
- import java.util.HashMap;
- import java.util.List;
-
- /**
- * author : Android 轮子哥
- * github : https://github.com/getActivity/AndroidProject
- * time : 2018/10/18
- * desc : 意图数据存取工具类
- */
- public final class IntentExtraUtils {
-
- private static Class<?> sCurrentClass;
-
- private static IntentExtraUtils sInstance;
-
- private static HashMap<String, Object> sMap;
-
- private IntentExtraUtils() {}
-
- public static IntentExtraUtils getInstance(Class cls) {
- if (sInstance == null) sInstance = new IntentExtraUtils();
- if (sMap == null) sMap = new HashMap<>();
- sCurrentClass = cls;
- return sInstance;
- }
-
- /**
- * 跳转到Activity
- *
- * @param context context对象
- */
- public IntentExtraUtils startActivity(Context context) {
- return startActivity(context, false);
- }
-
- /**
- * 跳转到Activity后再销毁当前Activity
- *
- * @param activity activity对象
- */
- public IntentExtraUtils startActivityFinish(Activity activity) {
- startActivity(activity, false);
- activity.finish();
- return this;
- }
-
- /**
- * 跳转到Activity
- *
- * @param context context对象
- * @param newTask 是否开启新的任务栈
- */
- public IntentExtraUtils startActivity(Context context, boolean newTask) {
- Intent intent = new Intent(context, sCurrentClass);
- if (newTask) {
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- }
- context.startActivity(intent);
- return this;
- }
-
- /**
- * 跳转到Activity
- *
- * @param activity activity对象
- * @param requestCode 请求码
- */
- public IntentExtraUtils startActivity(Activity activity, int requestCode) {
- activity.startActivityForResult(new Intent(activity, sCurrentClass), requestCode);
- return this;
- }
-
- public IntentExtraUtils startActivity(Fragment fragment, int requestCode) {
- fragment.startActivityForResult(new Intent(fragment.getActivity(), sCurrentClass), requestCode);
- return this;
- }
-
- public IntentExtraUtils startActivity(androidx.fragment.app.Fragment fragment, int requestCode) {
- fragment.startActivityForResult(new Intent(fragment.getActivity(), sCurrentClass), requestCode);
- return this;
- }
-
- /**
- * 设置结果码
- *
- * @param activity activity对象
- * @param resultCode 结果码
- */
- public IntentExtraUtils setResult(Activity activity, int resultCode) {
- activity.setResult(resultCode);
- return this;
- }
-
- /**
- * 销毁Activity
- *
- * @param activity activity对象
- */
- public void finish(Activity activity) {
- activity.finish();
- }
-
- // Object
-
- public IntentExtraUtils put(Class<?> clazz, Object object) {
- return put(sCurrentClass + clazz.getName(), object);
- }
-
- public IntentExtraUtils put(String key, Object object) {
- sMap.put(key, object);
- return this;
- }
-
- public <T extends Object> T get(Class<T> clazz) {
- return get(sCurrentClass + clazz.getName());
- }
-
- public <T extends Object> T get(String key) {
- T t = (T) sMap.get(key);
- //移除这个对象,避免内存泄露
- sMap.remove(key);
- return t;
- }
-
- // String
-
- public IntentExtraUtils putString(String s) {
- return put(String.class, s);
- }
-
- public IntentExtraUtils putString(String key, String s) {
- return put(key, s);
- }
-
- public String getString() {
- return get(String.class);
- }
-
- public String getString(String key) {
- return get(key);
- }
-
- // Integer
-
- public IntentExtraUtils putInteger(Integer i) {
- return put(Integer.class, i);
- }
-
- public IntentExtraUtils putInteger(String key, Integer i) {
- return put(key, i);
- }
-
- public Integer getInteger() {
- return get(Integer.class);
- }
-
- public Integer getInteger(String key) {
- return get(key);
- }
-
- // Long
-
- public IntentExtraUtils putLong(Long l) {
- return put(Long.class, l);
- }
-
- public IntentExtraUtils putLong(String key, Long l) {
- return put(key, l);
- }
-
- public Long getLong() {
- return get(Long.class);
- }
-
- public Long getLong(String key) {
- return get(key);
- }
-
- // Boolean
-
- public IntentExtraUtils putBoolean(Boolean b) {
- return put(Boolean.class, b);
- }
-
- public IntentExtraUtils putBoolean(String key, Boolean b) {
- return put(key, b);
- }
-
- public Boolean getBoolean() {
- return get(Boolean.class);
- }
-
- public Boolean getBoolean(String key) {
- return get(key);
- }
-
- // Double
-
- public IntentExtraUtils putDouble(Double d) {
- return put(Double.class, d);
- }
-
- public IntentExtraUtils putDouble(String key, Double d) {
- return put(key, d);
- }
-
- public Double getDouble() {
- return get(Double.class);
- }
-
- public Double getDouble(String key) {
- return get(key);
- }
-
- // Float
-
- public IntentExtraUtils putFloat(Float f) {
- return put(Float.class, f);
- }
-
- public IntentExtraUtils putFloat(String key, Float f) {
- return put(key, f);
- }
-
- public Float getFloat() {
- return get(Float.class);
- }
-
- public Float getFloat(String key) {
- return get(key);
- }
-
- // List
-
- public IntentExtraUtils putList(List list) {
- return put(List.class, list);
- }
-
- public IntentExtraUtils putList(String key, List list) {
- return put(key, list);
- }
-
- public List getList() {
- return get(List.class);
- }
-
- public List getList(String key) {
- return get(key);
- }
-
- public static class Key {
-
- public static final String ID = "id"; // id
- public static final String TOKEN = "token"; // token
- public static final String ORDER = "order"; // 订单
- public static final String BALANCE = "balance"; // 余额
- public static final String TIME = "time"; // 时间
- public static final String CODE = "code"; // 错误码或者其他码
- public static final String URL = "url"; // URL
- public static final String PATH = "path"; // 路径
- public static final String OTHER = "other"; // 其他
-
- // 个人信息
- public static final String NAME = "name"; // 姓名
- public static final String AGE = "age"; // 年龄
- public static final String SEX = "sex"; // 性别
- public static final String PHONE = "phone"; // 手机
- public static final String VIP = "vip"; // 会员
- public static final String DESCRIBE = "describe"; // 描述
- public static final String REMARK = "remark"; // 备注
- public static final String CONSTELLATION = "constellation"; // 星座
-
- // 地方
- public static final String ADDRESS = "address"; // 地址
- public static final String PROVINCE = "province"; // 省
- public static final String CITY = "city"; // 市
- public static final String DISTRICT = "district"; // 区
-
- // 文件类型相关
- public static final String TXT = "txt"; // 文本
- public static final String PICTURE = "picture"; // 图片
- public static final String VOICE = "voice"; // 音频
- public static final String VIDEO = "video"; // 视频
-
- // 支付相关
- public static final String BALANCE_PAY = "balance_pay"; // 余额支付
- public static final String WECHAT_PAY = "wechat_pay"; //微信支付
- public static final String ALI_PAY = "ali_pay"; //支付宝支付
- public static final String UNION_PAY = "union_pay"; // 银联支付
- }
- }
|