Android4-2_ Intent란? Implicit Intent 암시적 인텐트
11분 11초-14분 50초
암시적 인텐트 Implicit Intent
암시적 인텐트로 다른 액티비티를 시작시키기 위해서는 인텐트 안에 작업과 데이터를 지정해야한다.
예를들어 114번호로 다이얼 작업을 수행할 수 있는 액티비티를 실행 시키기 위해서는 다음과 같이 인텐트를 생성하고 이를 startActivity() 메소드에 전달하면 된다.
▼코드
val call_intent = intent(intent.Action_DIAL, Uri.parse("tel:114"))
startActivity(call_intent)
여기서,
val call_intent = intent(intent.Action_DIAL, Uri.parse("tel:114"))
Action_DIAL은 다이얼 작업이고,
("tel:114")는 전화번호 114의 uri 객체이다.
activity_first.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=".FirstActivity">
<TextView
android:id="@+id/textView"
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" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="두번째 액티비티 시작하기!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/buttonDialActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="다이얼 작업 시작하기"
android:onClick="doOnBtnclick"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1" />
<Button
android:id="@+id/buttonMapAcitivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="지도보기 작업 시작하기"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonDialActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
FirstActivity.kt
package com.example.myapplication
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
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()){
R.id.buttonDialActivity ->{
val call_intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:114"))
startActivity(call_intent)
}
R.id.buttonMapAcitivity -> {
val map_intent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:37.565350,127.01445"))
startActivity(map_intent)
}
}
}
}
여기서, FirstActivity.kt의 함수값을 doOnBtnClick으로 주고
이 버튼 두개를 한번에 묶은 건데,
이래도 되나 싶겠지만, 된다.
왜냐면 아이디로 구분을 할것이기 때문이다.
id가 각각 buttonDialActivity 와 buttonMapActivity로 나뉘어져 있음.
구분한 것은
when구문으로
when(view.getId()){
R.id.buttonDialActivity ->{
val call_intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:114"))
startActivity(call_intent)
}
R.id.buttonMapAcitivity -> {
val map_intent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:37.565350,127.01445"))
startActivity(map_intent)
}
}
이렇게 구분되어있다. // getId를 써서 구분했음
buttonDialActivity 이 눌렸을 때는, ACTION_DIAL해서 114로 연결이 되고
buttonMapActivity이 눌렸을 때는 ACTION_VIEW에다가 위도 , 경도 위치 정보를줄거에요.
플레이했는데 안되길래 뭐가문젠가 봤더니 ㅠ
잘못썼다.
xml에는
아까 doOnBtnclick이라고 C를 소문자로 썼는데, .kt 파일에는 대문자로 써서 실행이 안됐..
변경했더니 잘 작동한다!!