ブラウザの起動

Kotlin

暗黙的Intent

起動するアクティビティを明記しないIntentを暗黙的インテントと呼びます

地図アプリを起動すると「GoogleMap、YahooMapなどからどれを開くか選択する画面」が出てきますが、このようにユーザーに起動するアプリを選択させる場合は、暗黙的インテントを使います

MainActivity.kt

import android.content.intent
import android.net.Uri

class MainActivity : AppCompatActivity() {
    override fun onCreate(saveInstanceStates: Bundle?) {
       ... 省略 ...
       val button = findViewById<Button>(R.id.button)
       button.setOnClickListener {
           openGoogle()
       }
    }

    fun openGoogle() {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com/"))
        startActivity(intent)
    }

}

Intent()の第2引数にはこれから起動するアクティビティを書きます

Intent.ACTION_VIEW … 画面を表示させる

Intent.ACTION_CALL … 電話をかける

Intent.ACTION_SEND … メールを送信する

BACK