Android/Java การ share ข้อมูลด้วย Intent.ACTION_SEND

การ share ข้อมูลออกไปให้แอพอื่นด้วย Intent.ACTION_SEND เช่น ส่งไปแอพอีเมล แอพไลน์

ใส่ข้อความที่จะส่งออก แล้วกด Share
เลือกว่าจะ share ให้แอพไหน

ไฟล์ที่เกี่ยวข้อง

  • activity_main.xml
  • MainActivity.java

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="E-mail content"
            android:text="Hello World!" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Share" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.phaisarn.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"email@example.com"});
                intent.putExtra(Intent.EXTRA_SUBJECT, "Export data");

                EditText editText = findViewById(R.id.editText);
                String bodyText = editText.getText().toString();
                intent.putExtra(Intent.EXTRA_TEXT, bodyText);

                startActivity(Intent.createChooser(intent, "Share to ..."));
            }
        });
    }
}

บรรทัดที่ 22 สร้าง intent แบบ Intent.ACTION_SEND
บรรทัดที่ 23 กำหนดชนิดข้อมูลที่จะส่งเป็น text/plain
บรรทัดที่ 24 ใส่ข้อมูลอีเมล Intent.EXTRA_EMAIL ถ้าส่งไปแอพเมลอันนี้จะเป็น mailTo
บรรทัดที่ 25 ใส่ข้อมูลหัวข้อ Intent.EXTRA_SUBJECT ถ้าส่งไปแอพเมลอันนี้จะเป็น subject
บรรทัดที่ 29 ใส่ข้อมูล text Intent.EXTRA_TEXT ถ้าส่งไปแอพเมลอันนี้จะเป็นเนื้อหาอีเมล ถ้าส่งไป Line อันนี้จะเป็นข้อความที่ส่งต่อ
บรรทัดที่ 31 Intent.createChooser ให้ผู้เใช้เลือกว่าจะให้ส่งข้อมูลไปแอพไหน