Android

๋‹ค์–‘ํ•œ ์ธํ…ํŠธ Intent (์—ฐ๋ฝ์ฒ˜, ์›น, ๋ฌธ์ž, ์ด๋ฉ”์ผ, ๊ณต์œ ) ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

567Rabbit 2024. 6. 11. 17:33
package com.~.intent;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {

    Button button;

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

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // ์—ฐ๋ฝ์ฒ˜ ์„ ํƒํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ ๋„์šฐ๊ธฐ
                // selectContact();

                // ์›น ๋ธŒ๋ผ์šฐ์ € ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜
                // openWebPage("http://naver.com");

                // SMS ์ž‘์„ฑํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜
                // composeSMS("010-2222-3333");

                // ์ด๋ฉ”์ผ ์ž‘์„ฑํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜
                // composeEmail(new String[]{"abc@naver.com"},"์ด๋ฉ”์ผ ์ œ๋ชฉ");

                // ๊ณต์œ ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ์„œ, ๋ฌธ์ž์—ด์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜
                // shareText("์•ˆ๋…•ํ•˜์„ธ์š”~~");
            }
        });
    }

    // ์—ฐ๋ฝ์ฒ˜ ์„ ํƒํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ ๋„์šฐ๊ธฐ
    void selectContact(){
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivity(intent);
    }

    // ์›น ๋ธŒ๋ผ์šฐ์ € ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜
    void openWebPage(String urI){
        Uri uri = Uri.parse(urI);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    // SMS ์ž‘์„ฑํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜
    void composeSMS(String phone){
        Uri uri = Uri.parse("smsto:" + phone);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    // ์ด๋ฉ”์ผ ์ž‘์„ฑํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ํ•จ์ˆ˜
    void composeEmail(String[] address, String subject){
        Uri uri = Uri.parse("mailto:");
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(uri);
        intent.putExtra(Intent.EXTRA_EMAIL, address);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        startActivity(intent);
    }

    // ๊ณต์œ ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ์„œ, ๋ฌธ์ž์—ด์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ•จ์ˆ˜
    void shareText(String text){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.setType("text/plain");

        Intent shareIntent = Intent.createChooser(intent, "์„ ํƒํ•˜์„ธ์š”");
        startActivity(shareIntent);

    }
}