Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 패스트캠퍼스
- 인스턴스
- SizedBox
- 상속
- 변수
- 반복문
- Widget
- Kotlin
- If
- padding
- appbar
- StatelessWidget
- spacer
- 리스트뷰
- 뷰바인딩
- button
- StatefulWidget
- 두 수의 나눗셈
- setState
- 안드로이드 스튜디오
- 지연초기화
- 람다식
- expanded
- Flutter
- 액티비티 생명주기
- 프래그먼트
- margin
- 빌드 프로세스
- 추상메소드
- 프로그래머스
Archives
- Today
- Total
Y_Ding
내배캠 Android TIL - Intent 본문
인텐트(Intent)
- 1이라는 액티비티에서 2라는 액티비티를 호출하고 싶다면? 인텐트를 사용하면 됨
- 명시적 인텐트와 암시적 인텐트로 나눌 수 있음
명시적 인텐트(Explicit Intent)
- 내가 호출할 액티비티를 알고 호출하는 것
- 보통 현재 앱 안에 있는 구성요소를 시작시킬 때 사용
- startActivity() 메소드로 호출하고, 시작하고자 하는 액티비티를 설명하는 Intent 객체를 전달
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
* this 가 애플리케이션 컨텍스트 정보 넘겨줌
* SecondActivity 시작하고자 하는 액티비티 클래스
명시적 인텐트 실습하기 - 버튼을 누르면 다른 화면을 호출하는 실습을 해보자!
FirstActivity.kt
class FirstActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
val btn = findViewById<Button>(R.id.btn1)
// btn1은 activity_fist.xml 파일에서 버튼에게 지정한 id명
btn.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
}
SecondActivity.kt
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val btn = findViewById<Button>(R.id.btn_close)
//btn_close는 activity_second.xml파일에서 버튼에 지정해준 id명
btn.setOnClickListener {
finish()
}
}
}
암시적 인텐트(Implicit Intent)
- 예를 들어 내가 A라는 Activity를 만들고 거기서 인터넷 브라우저를 띄우고 싶음
- 그런데 내 핸드폰에 설치된 앱들 중에서 인터넷 브라우저를 띄우는 앱이 여러개(인터넷앱, chrome...)
- 암시적 인텐트를 통해서 안드로이드 시스템이 인터넷 브라우저를 띄워줄 수 있는 앱들을 찾아냄
val call_intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:114"))
startActivity(call_intent)
* ACTION_DIAL 은 다이얼 작엄
* "tel: 114" 는 전화번호 114의 URI 객체를 의미!
암시적 인텐트 실습하기 - 버튼을 누르면 전화걸기와 지도보기 화면으로 넘어가기!
class FirstActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
val btn = findViewById<Button>(R.id.btn1)
btn.setOnClickListener {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
fun doOnBtnClick(view: View){
when(view.getId()){
//getId()를 통해 view의 id를 가져온다.
R.id.buttonDialActivity -> {
// 114 전화번호로 다이얼 작업을 수행할 수 있도록 인텐트 설정
val call_intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:114"))
startActivity(call_intent)
}
R.id.buttonMapActivity -> {
// 주어진 위도,경도 위치로 지도를 보여줄 수 있도록 인텐트 설정
val map_intent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:37.565350, 127.01445"))
startActivity(map_intent)
}
}
}
}
}
*view : 클릭된 버튼 객체
클릭된 버튼 객체가 무엇인지를 id를 통해 인지하며, 두가지 다른 인텐트 객체를 생성
activity_first.xml
<Button
android:id="@+id/buttonDialActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="doOnBtnClick"
android:text="다이얼 작업 시작하기"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1" />
<Button
android:id="@+id/buttonMapActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="doOnBtnClick"
android:text="지도보기 작업 시작하기"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonDialActivity" />
xml파일을 보면 다이얼 작업 시작하기 버튼과 지도보기 작업 시작하기 버튼의 onClick에 들어간 이름이 똑같이 doOnBtnClick으로 되어있는 것을 알 수있다.
둘 중 하나의 버튼을 누르면 doOnBtnClick의 함수가 실행되는데 구분을 해주지 않아도 되는걸까?
결론은 상관이 없다.
왜냐? getId()를 통해 view의 id를 받아오게 되기 때문이다.
그래서 각 버튼의 id인 buttonDialActivity 나 buttonMapActivity를 통해 구분해서 실행될 수 있도록 한다.
암시적 인텐트 수신하기 실습
Manifests file - SecondActivity
<activity
android:name=".SecondActivity"
android:exported="false">
//intent필터 추가
// secondactivity에 intent필터를 추가할건데
// 암시적 인텐트를 받을건데 다이얼 관련된 것을 받을거야
<intent_filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent_filter>
//intent필터 추가
</activity>
소소한 Tip.. 코드정렬 단축키: Ctrl + Alt + L
Intent의 객체
- 컴포넌트 이름 : 명시적 인텐트일 경우는 지정한 이름을 넣어주지만, 지정되지 않으면 암시적 인텐트
- 작업(Action) : 수행되어야 할 작업을 나타내는 문자열 (전화통화 수행, 데이터를 표시, 보내기...)
- 데이터: 작업에 필요한 데이터 (예: ACTION_VIEW, ACTION_CALL, ACTION_DIAL...)
- 카테고리 : 작업에 대한 추가적인 정보 (예: CATEGORY_BROWSABLE, CATEGORY_LAUNCHER)
- 엑스트라 : 요청한 작업을 수행하기 위해 필요한 추가정보를 담고있는 키-값 쌍의 데이터
Today!
실습을 하면서 명시적 인텐트와 암시적 인텐트에 대한 구분이 잘 되는 것 같은 기분이 들었는데 근데 또 알 것 같으면서도 모르겠는 기분이다.
이건 그냥 모르는 게 맞다고 해야겠지..ㅋㅋㅋㅋㅋ
머리로는 그려지는데 말로는 설명이 되지 않는다.
정확한 개념을 입으로 말할 수 있을 때까지 추가적인 공부가 필요해!
'TodayILearned > Android&Kotlin' 카테고리의 다른 글
내배캠 개인과제 TIL (0) | 2023.08.10 |
---|---|
내배캠 개인과제 TIL (08.08~08.09) (2) | 2023.08.09 |
내배캠 Android TIL - Activity (1) | 2023.08.04 |
내배캠 Android TIL - Layout (0) | 2023.08.03 |
내배캠 Android TIL - Widget (0) | 2023.08.03 |