Activity 之间值得传递方式
发布日期:2021-06-23 19:02:39
浏览次数:12
分类:技术文章
本文共 6811 字,大约阅读时间需要 22 分钟。
- Intent传值
//1.Intent 传递数据Intent intent = new Intent(this, MyViewActivity.class);intent.putExtra("str_data", "0");//字符串intent.putExtra("int_data", 0);//数值类型startActivity(intent);//获取值int rs_num = getIntent().getIntExtra("int_data", 0);String rs_str = getIntent().getStringExtra("str_data");
- 静态变量可以通过类成员传递数据
public static final String TAG = "ABC";
- 剪切板传递数据
//值得写入Intent intent = new Intent(this, MyViewActivity.class);//获得剪切板对象ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);clipboardManager.setText("复制到剪切板的值");startActivity(intent);//剪切板值读取String str2 = clipboardManager.getText().toString();
扩展:
剪切板传递对象
void personCutData() { //自定义一个对象 MyData md = new MyData("张三", 26); //定义字符串 String baseToString = ""; //1.将对象转换成字符串 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(md); baseToString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); objectOutputStream.close(); } catch (Exception e) { e.printStackTrace(); }}void getPersonCutData() { String returnString; ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //将base64转换成toString byte[] base64ToString = Base64.decode(clipboardManager.getText().toString(), Base64.DEFAULT); //从流中读取数据 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64ToString); try { ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); MyData md = (MyData) objectInputStream.readObject(); objectInputStream.close(); } catch (Exception e) { e.printStackTrace(); }}public class MyData { public MyData(String name, int age) { }}
在实际开发中 SharedPreferences 虽然不属于数据传递,但是activity之间数据可以通过存储取出的方式,另外SQLite 手机数据库也是同理。下面附上相关内容。
package com.zhy.utils;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Map;import android.content.Context;import android.content.SharedPreferences;public class SPUtils{ /** * 保存在手机里面的文件名 */ public static final String FILE_NAME = "share_data"; /** * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 * * @param context * @param key * @param object */ public static void put(Context context, String key, Object object) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); if (object instanceof String) { editor.putString(key, (String) object); } else if (object instanceof Integer) { editor.putInt(key, (Integer) object); } else if (object instanceof Boolean) { editor.putBoolean(key, (Boolean) object); } else if (object instanceof Float) { editor.putFloat(key, (Float) object); } else if (object instanceof Long) { editor.putLong(key, (Long) object); } else { editor.putString(key, object.toString()); } SharedPreferencesCompat.apply(editor); } /** * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 * * @param context * @param key * @param defaultObject * @return */ public static Object get(Context context, String key, Object defaultObject) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); if (defaultObject instanceof String) { return sp.getString(key, (String) defaultObject); } else if (defaultObject instanceof Integer) { return sp.getInt(key, (Integer) defaultObject); } else if (defaultObject instanceof Boolean) { return sp.getBoolean(key, (Boolean) defaultObject); } else if (defaultObject instanceof Float) { return sp.getFloat(key, (Float) defaultObject); } else if (defaultObject instanceof Long) { return sp.getLong(key, (Long) defaultObject); } return null; } /** * 移除某个key值已经对应的值 * @param context * @param key */ public static void remove(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); SharedPreferencesCompat.apply(editor); } /** * 清除所有数据 * @param context */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); SharedPreferencesCompat.apply(editor); } /** * 查询某个key是否已经存在 * @param context * @param key * @return */ public static boolean contains(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.contains(key); } /** * 返回所有的键值对 * * @param context * @return */ public static MapgetAll(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.getAll(); } /** * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类 * * @author zhy * */ private static class SharedPreferencesCompat { private static final Method sApplyMethod = findApplyMethod(); /** * 反射查找apply的方法 * * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Method findApplyMethod() { try { Class clz = SharedPreferences.Editor.class; return clz.getMethod("apply"); } catch (NoSuchMethodException e) { } return null; } /** * 如果找到则使用apply执行,否则使用commit * * @param editor */ public static void apply(SharedPreferences.Editor editor) { try { if (sApplyMethod != null) { sApplyMethod.invoke(editor); return; } } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } editor.commit(); } }}
上面工具类出处
关于数据库技术用郭神的框架
Android数据库高手秘籍专栏
转载地址:https://blog.csdn.net/u014513456/article/details/50721919 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年03月18日 14时52分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
node与mysql开源_node与mysql的相互使用————node+mysql
2019-04-21
mysql连接nginx_nginx四层负载均衡连接mysql
2019-04-21
mysql截取栏目字符_substring从指定字符串开始截取(图)
2019-04-21
python类属性初始化_Python类定义、属性、初始化和析构
2019-04-21
mysql构建url给scrapy_Python Scrapy从mysq填充起始url
2019-04-21
owdcloud mysql_MySQL在Ubuntu远程配置
2019-04-21
python基础装饰器_Python基础 装饰器及练习
2019-04-21
python导出csv不带引号的句子_不带双引号写入CSV文件
2019-04-21
python爬虫代码模板_Python:学习Python爬虫的第一天
2019-04-21
springboot获取原生js请求_springboot跳转原生html
2019-04-21
java buffer nio_Java NIO之Buffer(缓冲区)入门
2019-04-21
android java加密_android 和java平台通用的AES加密解密
2019-04-21
java导出类_java导出excel工具类
2019-04-21
java学习手册下载_Java学习手册
2019-04-21
axios delete有请求体吗_关于axios请求——delete方法
2019-04-21