개발학습일지

[Android Studio] Intent 를 활용해서 외부 앱 엑티비티 띄우기 _ 연락처, 웹 브라우저, SMS, email, 공유하기 본문

Android Studio

[Android Studio] Intent 를 활용해서 외부 앱 엑티비티 띄우기 _ 연락처, 웹 브라우저, SMS, email, 공유하기

처카푸 2024. 6. 12. 12:32

Intent를 활용해서 외부 앱 액티비티 띄우기

 

1. 연락처 선택하는 액티비티 실행시키는 함수

   void selectContact(){
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivity(intent);
    }

- 함수 사용

selectContact();

 


2. 웹 브라우저 액티비티 실행시키는 함수

    void openWebPage(String url){
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

- 함수 사용

openWebPage("www.naver.com");

 


3. sms 문자 작성하는 액티비티를 실행시키는 함수

    void composeSMS(String phone){
        Uri uri = Uri.parse("smsto:"+phone);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

- 함수 사용

composeSMS("010-0000-2222");

 


4. 이메일 작성하는 액티비티를 실행시키는 함수

    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);
    }

- 함수 사용

composeEmail(new String[]{"abc@naver.com"}, "테스트 메일");

 


5. 공유버튼 눌러서, 문자열을 공유할 수 있도록 하는 함수

    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);
    }

- 함수 사용

shareText("안녕하세요!!");

 


* 이 외의 공유 인텐트 ..

https://developer.android.com/training/sharing/send?hl=ko#send-text-content

 

다른 앱으로 간단한 데이터 전송  |  Android Developers

인텐트를 구성할 때 인텐트가 '트리거'할 작업을 지정해야 합니다. Android는 짐작할 수 있듯이 인텐트가 하나의 활동에서 데이터를 전송하고 있음을 나타내는 ACTION_SEND를 비롯한 여러 작업을 정

developer.android.com