Android 使用剪贴板传递简单数据及复杂数据的方法

今日长缨在手,何时缚住苍龙。这篇文章主要讲述Android 使用剪贴板传递简单数据及复杂数据的方法相关的知识,希望能为你提供帮助。
传递数据的场景在于不同页面之间跳转,需要携带数据:简单数据值指的是String, int等数据, 复杂数据指的是类
 
1.   使用剪贴板传递简单数据方法:
【Android 使用剪贴板传递简单数据及复杂数据的方法】第一个页面里面放数据操作如下:

1ClipboardManager clipboardManager = (ClipboardManager); 2getSystemService(Context.CLIPBOARD_SERVICE); 3String text = "简单数据"; 4clipboardManager.setText(text); 5Intent intent = new Intent(this, OtherActivity.class); 6startActivity(intent);

第二个页面里面取数据操作如下:
Intent intent = getIntent(); textView = findViewById(R.id.msgText); myApp = (MyApp) getApplication(); textView.setText("after changed :" + myApp.getText());

  其中MyApp是一个类文件,里面如下:
public class MyApp extends Application {private String text; public String getText() { return text; }public void setText(String text) { this.text = text; }}

在manifest文件中加入MyApp类:
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.xxx.globalvariables"> < application android:name=".MyApp" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < activity android:name=".MainActivity"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < activity android:name=".OtherActivity"> < /activity> < /application> < /manifest>

 
  2.   使用剪贴板传递复杂数据传递方法
第一个页面存数据的操作如下:
// 方法二:剪贴板传递复杂数据 MyData myData = https://www.songbingjia.com/android/new MyData("jack", 24); // 将对象转换为字符串 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); String base64String = ""; try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(myData); base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); // 加密 objectOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } ClipboardManager clipboardManager1 = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager1.setText(base64String); Intent intent1 = new Intent(this, OtherActivity.class); startActivity(intent1);

  第二个页面取数据的方法:
Intent intent = getIntent();


ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); String msg = clipboardManager.getText().toString(); ClipBoardTextView = findViewById(R.id.ClipBoardMsgText); // 解码 byte[] base64_byte = Base64.decode(msg, Base64.DEFAULT); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(byteArrayInputStream); MyData myData = https://www.songbingjia.com/android/(MyData) objectInputStream.readObject(); ClipBoardTextView.setText(myData.toString()); } catch (Exception e) { e.printStackTrace(); }

  MyData是一个类, 里面包括name, age 的get方法和toString()方法, 该类需要实现
Serializable

备注:由于该类为普通类, 没有继承

Application, 所以不用再manifest文件中配置!


 




    推荐阅读