0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Androidで別のアプリを起動する

0
Posted at

Androidで別アプリを起動する方法をメモします。

パッケージ名を指定して起動する

別アプリの起動画面を開く場合は、getLaunchIntentForPackage() を使用します。

val packageName = "com.example.app"
val intent = packageManager.getLaunchIntentForPackage(packageName)

if (intent != null) {
    startActivity(intent)
}

特定のActivityを起動する

起動先の特定のActivityを起動したい場合はクラス名を指定します。

起動元
val intent = Intent().apply {
    setClassName(
        "com.example.app",
        "com.example.app.MainActivity"
    )
}

startActivity(intent)

この方法は、起動先の Activity が外部アプリから起動できるように公開されている必要があります。

起動先
<permission
    android:name="com.example.app.permission.OPEN_ACTIVITY"
    android:protectionLevel="signature" />

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:permission="com.example.app.permission.OPEN_ACTIVITY" />

起動時にデータを渡す

起動時にputExtra() を使えば、起動先アプリに文字列などのデータを渡せます。

起動元
val intent = packageManager
    .getLaunchIntentForPackage("com.example.app")

if (intent != null) {
    intent.putExtra("message", "Hello")
    startActivity(intent)
}
起動先
val message = intent.getStringExtra("message")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?