使用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

13. Proxy

13. Proxy

git 常用技巧 -- gitignore

git 常用技巧 -- gitignore

用 Express & Sequelize 打造 MVC 餐廳網站(上)

用 Express & Sequelize 打造 MVC 餐廳網站(上)


Comments