使用Intent開啟APP


Posted by imcloudwu on 2021-11-05

Reciever manifest sample

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

Get data

if(intent?.action == Intent.ACTION_SEND){
    if ("text/plain" == intent.type) {
        val text = intent.getStringExtra(Intent.EXTRA_TEXT)
        println(text)
    }
}

Send data

val sendIntent: Intent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
    type = "text/plain"
}

startActivity(sendIntent)

URL Scheme

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="myapp" android:host="www.test.com" />
</intent-filter>
val uri: Uri = Uri.parse("myapp://www.test.com")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)









Related Posts

SQL查詢語句執行順序

SQL查詢語句執行順序

[JS] 浮點數精度問題

[JS] 浮點數精度問題

Day 20 & 21-Snake Game

Day 20 & 21-Snake Game


Comments