본문 바로가기
Android/Concepts

Intent란?

by JuHy_ 2020. 3. 31.

Intent란?

Intent는 컴포넌트 간의 통신을 위해 만들어진 메신저이다.

이 때 Intent의 목적지를 정확하게 지정해 줄 경우 이를 명시적 Intent라고 한다.

 

그렇다면 목적지가 없이 이동할 수도 있다는 건데 그건 무슨 뜻일까?

예를 들어 위와 같은 특정 action을 지정하여 Intent를 사용할 수도 있는데 이를 암시적 Intent라고 한다.

암시적 Intent는 지정된 action을 수행할 수 있는 앱을 찾아 알아서 이동하게 된다.

 

 

명시적 Intent 사용법

명시적 Intent는 Intent 객체를 생성하여 목적지를 설정해준 뒤 startActivity() 함수를 통해 이동 가능하다.

자세한 내용은 Activity를 설명하는 글에서 앞서 다루었으니 참고하자.

 

https://ju-hy.tistory.com/44

 

Activity란?

Activity란? 안드로이드 앱은 크게 Activity, Service, Broadcast Receiver, Content Provider 4가지 컴포넌트로 구성된다. 각각의 컴포넌트들의 기능에 대해 간단히 설명하자면 Activity는 화면에 보여지는 부분..

ju-hy.tistory.com

 

※ ComponentName을 이용한 목적지 지정 방법

Intent intent = new Intent();
ComponentName name = new ComponentName("com.juhy.myapplication", "com.juhy.myapplication.NewActivity");
intent.setComponent(name);
startActivity(intent);

ComponentName 객체를 생성하여, 여기에 패키지명과 이동하려는 클래스명을 입력해 준 뒤,

intent에 setComponent() 함수를 통해 만들어준 ComponentName 객체를 지정해준다.

그 다음 startActivity()를 통해 원하는 목적지로 이동할 수 있다.

 

 

암시적 Intent 사용법

암시적 intent를 사용하기 위해서는 목적지가 아닌 action을 지정해주어야 한다.

전화걸기를 예를 들어 구현해보자.

 

<?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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="전화걸기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

먼저 전화번호를 입력받을 EditText와 Button을 하나 만들어주자.

 

package com.juhy.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    EditText editText;

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

        editText = findViewById(R.id.editText);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String receiver = editText.getText().toString();

                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + receiver));
                startActivity(intent);
            }
        });

    }

}

그 다음 버튼을 눌렀을 때 EditText에서 번호를 가져오고,

Intent에 전화걸기를 의미하는 ACTION_DIAL과, tel이라는 키워드에 입력받은 전화번호를 넣어준다.

그 다음 startActivity() 함수에 intent를 넣어주면 된다.

 

실행 후 번호를 입력한 뒤 전화걸기를 눌러보면 정상적으로 전화 앱이 실행되고 번호가 전달된 것을 볼 수 있다.

 

 

Reference

[부스트코스]안드로이드 프로그래밍

https://www.edwith.org/boostcourse-android

'Android > Concepts' 카테고리의 다른 글

Intent를 이용한 Activity 간 데이터 전달  (0) 2020.04.03
Intent의 Flag 사용법  (0) 2020.04.02
Activity란?  (0) 2020.03.31
GridView 사용법  (0) 2020.03.24
Spinner 사용법  (0) 2020.03.24