1. 程式人生 > >Android 使用剪貼板傳遞簡單數據及復雜數據的方法

Android 使用剪貼板傳遞簡單數據及復雜數據的方法

一個 nts 數據的操作 gpo cat div 其中 sta all

傳遞數據的場景在於不同頁面之間跳轉,需要攜帶數據:簡單數據值指的是String, int等數據, 復雜數據指的是類

1. 使用剪貼板傳遞簡單數據方法:

第一個頁面裏面放數據操作如下:

1   ClipboardManager clipboardManager = (ClipboardManager);
2   getSystemService(Context.CLIPBOARD_SERVICE);         
3   String text = "簡單數據";
4   clipboardManager.setText(text);
5   Intent intent = new
Intent(this, OtherActivity.class); 6 startActivity(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 = 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 = (MyData) objectInputStream.readObject(); ClipBoardTextView.setText(myData.toString()); } catch (Exception e) { e.printStackTrace(); }

MyData是一個類, 裏面包括name, age 的get方法和toString()方法, 該類需要實現

Serializable

備註:由於該類為普通類, 沒有繼承
Application, 所以不用再manifest文件中配置!

Android 使用剪貼板傳遞簡單數據及復雜數據的方法