Implicit Intent

ewillwin·2023년 4월 12일
0

Intent

  • An Intent is a messaging object you can use to request an action from another app component.
    • Starting an activity
    • Starting a service
    • Delivering a broadcast

Activity Lifecycle

Android project

Android Manifest

  • Tell the android system the configuration of the application
    -> activity 추가할 경우, Manifest xml 파일에도 내용 추가해줘야함

Gradle

  • Tell the IDE the configuration of the project

Explicit Intent

  • Specify package name or component class name
  • Start an activity in Intent object
  • Data can be passed via Extras

Implicit Intent

  • Do not specify above name
  • Just general action
    • Call, Message, Location, ...

How it works?

-> Intent Filter

  • Declared general action: ACTION_VIEW, ACTION_CALL, ACTION_SEND...
  • Android System will choose candidate via Intent Filter
  • If no candidate: Fail
  • If multiple candidates: Let user choose (via system)

If you want to get implicit intent, (not send)

  • Let android system know we will get it
    • Intent filter, on AndroidManifest.xml

Receive with Broadcast Receiver

  • Receive Intent which is broadcasted from other places
  • Usually used when application does not need to react the message
  • ACTION_BOOT_COMPLETED, ACTION_LOCALE_CHANGED, ...
  • Notify to all targeted applications when special event happens

Receive with Activity

  • Receive Intent which is passed with startActivity() method
  • Used when Intent must be reacted via Activity
    • Usually used when application need to react

Broadcast

  • Sending message protocol which declared by Android system
  • Implicit Intent is exactly same with the broadcast message

Labpractice

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <queries>
        <intent>
            <action android:name="android.intent.action.SET_ALARM"/>
        </intent>
    </queries>
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Week7"
        tools:targetApi="31">
        <activity
            android:name=".NothingActivity"
            android:exported="false" />

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

MainActivity.kt

package edu.skku.cs.week7

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import java.sql.BatchUpdateException

class MainActivity : AppCompatActivity() {
    companion object{
        const val EXT_HOUR = "extra_key_hour"
        const val EXT_MINUTE = "extra_key_minute"
        const val EXT_DESCRIPTION = "extra_key_description"
    }

    override fun onRestart() {
        super.onRestart()

        val editTextTime = findViewById<EditText>(R.id.editTextTextPersonName)
        val editTextDescription = findViewById<EditText>(R.id.editTextTextPersonName2)


        editTextTime.text.clear()
        editTextDescription.text.clear()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnSecondActivity = findViewById<Button>(R.id.button)
        btnSecondActivity.setOnClickListener {
            val editTextTime = findViewById<EditText>(R.id.editTextTextPersonName)
            val editTextDescription = findViewById<EditText>(R.id.editTextTextPersonName2)

            val time = editTextTime.text.toString()
            val hour = time!!.split(":")[0]
            val minute = time!!.split(":")[1]
            val description = editTextDescription.text.toString()

            val intent = Intent(this, NothingActivity::class.java).apply {
                putExtra(EXT_HOUR, hour)
                putExtra(EXT_MINUTE, minute)
                putExtra(EXT_DESCRIPTION, description)
            }
            startActivity(intent)
        }


//        val btnStart = findViewById<Button>(R.id.buttonBrowse)
//        btnStart.setOnClickListener {
//            val urlEditText = findViewById<EditText>(R.id.editTextUrl)
//            val uri = Uri.parse("https://" + urlEditText.text.toString())
//            val webIntent = Intent(Intent.ACTION_VIEW, uri)
//            startActivity(webIntent)
//        }
    }
}

NothingActivity.kt

package edu.skku.cs.week7

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock
import android.widget.Button
import android.widget.TextView
import android.widget.Toast

class NothingActivity: AppCompatActivity() {
    companion object{
        const val EXT_HOUR = "extra_key_hour"
        const val EXT_MINUTE = "extra_key_minute"
        const val EXT_DESCRIPTION = "extra_key_description"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout)


        val hour = intent.getStringExtra(MainActivity.EXT_HOUR)?.toInt()
        val minute = intent.getStringExtra(MainActivity.EXT_MINUTE)?.toInt()
        val description = intent.getStringExtra(MainActivity.EXT_DESCRIPTION)

        val textView = findViewById<TextView>(R.id.textView3)
        textView.text = "Do you want to set alarm on time ${hour}:${minute} with description '${description}'?"

        val yesButton = findViewById<Button>(R.id.button2)
        yesButton.setOnClickListener{
            val intent = Intent(AlarmClock.ACTION_SET_ALARM).apply {
                putExtra(AlarmClock.EXTRA_MESSAGE, description)
                putExtra(AlarmClock.EXTRA_HOUR, hour)
                putExtra(AlarmClock.EXTRA_MINUTES, minute)
            }
            startActivity(intent)
            this.finish()
        }

        val noButton = findViewById<Button>(R.id.button3)
        noButton.setOnClickListener {
            finish()
        }
    }
}

activity_main.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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Time"
        app:layout_constraintBottom_toBottomOf="@+id/editTextTextPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/editTextTextPersonName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Description"
        app:layout_constraintBottom_toBottomOf="@+id/editTextTextPersonName2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/editTextTextPersonName2" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:inputType="time"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextPersonName2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="description"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>

layout.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=".MainActivity">


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="OK"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:text="CANCEL"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글