[Android] Intent

๐Ÿ’ก ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ํŽ˜์ด์ง€ ์ „ํ™˜๊ณผ ํŽ˜์ด์ง€๊ฐ„ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์˜ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” Intent์— ๋Œ€ํ•˜์—ฌ ํ•™์Šตํ•˜์˜€์Šต๋‹ˆ๋‹ค !

 

Intent

  • ์ธํ…ํŠธ๋Š” ์•ฑ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฌด์—‡์„ ํ•  ๊ฒƒ์ธ์ง€๋ฅผ ๋‹ด๋Š” ๋ฉ”์‹œ์ง€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
  • ๋ฉ”์‹œ์ง€๋Š” ์˜์‚ฌ์†Œํ†ต์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋ณด๋‚ด๊ณ  ๋ฐ›์œผ๋ฉฐ, ContentProvider๋ฅผ ์ œ์™ธํ•œ ์•ˆ๋“œ๋กœ์ด๋“œ 4๋Œ€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.
    • Activity, Service, BroadCast Receiver
  • ์ธํ…ํŠธ๋Š” ๊ฐ ์ปดํฌ๋„ŒํŠธ๊ฐ„ ํ†ต์‹ ์„ ๋งก๊ณ  ์žˆ์œผ๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์“ฐ์ž…๋‹ˆ๋‹ค.

Activity์˜ Intent

  • Activity๋Š” ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ธฐ๋ณธ์ ์ธ UI ์š”์†Œ์ž…๋‹ˆ๋‹ค.
  • Intent๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // ๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ Intent
        val intent = Intent(this, SecondActivity::class.java).apply {
            putExtra("message", "Hello, SecondActivity!") // ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
        }
        startActivity(intent) // SecondActivity๋ฅผ ์‹œ์ž‘
    }
}

// SecondActivity.kt
class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        // ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
        val message = intent.getStringExtra("message")
        findViewById<TextView>(R.id.textView).text = message // ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
    }
}

Service์˜ Intent

  • Service๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.
  • Intent๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ Intent
        val intent = Intent(this, MyService::class.java).apply {
            putExtra("inputExtra", "Hello, Service!")
        }
        startService(intent) // ์„œ๋น„์Šค ์‹œ์ž‘
    }
}

// MyService.kt
class MyService : Service() {
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val input = intent?.getStringExtra("inputExtra")

        // ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ์ˆ˜ํ–‰ (์˜ˆ: ์•Œ๋ฆผ ํ‘œ์‹œ)
        val notification = NotificationCompat.Builder(this, "CHANNEL_ID")
            .setContentTitle("Service")
            .setContentText(input)
            .setSmallIcon(R.drawable.ic_service)
            .build()

        startForeground(1, notification) // ํฌ์–ด๊ทธ๋ผ์šด๋“œ ์„œ๋น„์Šค๋กœ ์‹œ์ž‘

        return START_NOT_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
}

BroadcastReceiver์˜ Intent

  • BroadcastReceiver๋Š” ์‹œ์Šคํ…œ ์ „์—ญ์˜ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.
    • ๋ฐฐํ„ฐ๋ฆฌ ๋ถ€์กฑ, ๋„คํŠธ์›Œํฌ ๋ณ€๊ฒฝ๊ณผ ๊ฐ™์€ ํ™˜๊ฒฝ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฒคํŠธ
  • Intent๋ฅผ ํ†ตํ•ด์„œ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ์†ก์‹ ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์†ก์‹ 
        val intent = Intent("com.example.ACTION_SEND").apply {
            putExtra("message", "Hello, BroadcastReceiver!")
        }
        sendBroadcast(intent) // ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์†ก์‹ 
    }
}

// MyBroadcastReceiver.kt
class MyBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        // ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ์—์„œ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
        val message = intent.getStringExtra("message")

        // ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌ (์˜ˆ: ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ)
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
    }
}

// AndroidManifest.xml
<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="com.example.ACTION_SEND" />
    </intent-filter>
</receiver>

์ธํ…ํŠธ ์œ ํ˜•

  • ์ธํ…ํŠธ๋Š” ๋ช…์‹œ์  ์ธํ…ํŠธ์™€ ์•”์‹œ์  ์ธํ…ํŠธ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

๋ช…์‹œ์  ์ธํ…ํŠธ

  • ๋ช…์‹œ์  ์ธํ…ํŠธ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•  ๋Œ€์ƒ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜ํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
val intent = Intent(this@MainActivity, SecondActivity::class.java)
startActivity(intent)
  • ์œ„ ์ธํ…ํŠธ์—์„œ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌํ•  ๋Œ€์ƒ์„ SecondActivity๋กœ ์ง€์ •ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋ช…์‹œ์  ์ธํ…ํŠธ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•”์‹œ์  ์ธํ…ํŠธ

  • ์•”์‹œ์  ์ธํ…ํŠธ๋Š” ๋ช…์‹œ์  ์ธํ…ํŠธ์™€ ๋‹ฌ๋ฆฌ ์ „๋‹ฌ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ ํŠน์ •ํ•œ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋Œ€์‹  ํŠน์ •ํ•œ ์ž‘์—…์„ ์ง€์ •ํ•˜์—ฌ, ์ง€์ •ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์„ฑ์š”์†Œ(๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ)์— ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์œ„ ๊ณผ์ •์—์„œ startActivity๋ฅผ ํ†ตํ•ด์„œ ์•ˆ๋“œ๋กœ์ด๋“œ ์‹œ์Šคํ…œ์ด ์ง€์ •ํ•œ ์ธํ…ํŠธ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์ฐพ์•„์„œ ์ธํ…ํŠธ๋ฅผ ์ „๋‹ฌ์‹œ์ผœ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์•”์‹œ์  ์ธํ…ํŠธ์˜ ์ธํ…ํŠธ ํ•„ํ„ฐ

  • ์•”์‹œ์  ์ธํ…ํŠธ๋Š” ์ธํ…ํŠธ ํ•„ํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  • ์ด๋Š” AndroidManifest.xml์—์„œ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • <action> : name ํŠน์„ฑ์—์„œ ํ—ˆ์šฉ๋œ ์ธํ…ํŠธ ์ž‘์—…
    • <data> : ํ—ˆ์šฉ๋œ ๋ฐ์ดํ„ฐ ์œ ํ˜•
    • <category> : name ํŠน์„ฑ์—์„œ ํ—ˆ์šฉ๋œ ์ธํ…ํŠธ ์นดํ…Œ๊ณ ๋ฆฌ
<activity android:name="SecondActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        // ์•”์‹œ์  ์ธํ…ํŠธ์—์„œ๋Š” category.DEFAULT๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>
  • System์ด ๋งค๋‹ˆํŽ˜์ŠคํŠธ ํŒŒ์ผ์— ์ •์˜๋œ ์ธํ…ํŠธ ํ•„ํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ์š”์ฒญํ•  ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ์— ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋Š ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์„ ํƒํ•  ์ง€ ์„ ํƒ ์กฐ๊ฑด์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Intent ๊ฐ์ฒด

  • ์ธํ…ํŠธ ๊ฐ์ฒด๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ •๋ณด๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๊ตฌ์„ฑ์š”์†Œ ์ด๋ฆ„
    • ์ž‘์—…
    • ๋ฐ์ดํ„ฐ
    • ์นดํ…Œ๊ณ ๋ฆฌ
    • ์—‘์ŠคํŠธ๋ผ
    • ํ”Œ๋ž˜๊ทธ

์ฐธ๊ณ 

https://velog.io/@akcineg/์•ˆ๋“œ๋กœ์ด๋“œ-์ธํ…ํŠธIntent

https://developer.android.com/guide/components/intents-filters?hl=ko