0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

パッケージ名からアプリ名(AndroidManifestのlabel属性のもの)を取得する

Last updated at Posted at 2020-06-28

暗黙的Intentで選択したアプリ名を使いたくて、パッケージ名からアプリ名を取得する必要があったため備忘録含め以下に展開します。

こちらのgetApplicationLabelを使うと以下のようになります。
loadLabelでも可

なお、パッケージ名はいくつか取得方法があると思いますがいつかよく使う、URLをブラウザで開くケースを載せておきます。

Case1.kt
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)
    val launchIntent = Intent(Intent.ACTION_VIEW)
    val url = "https://www.google.com/?hl=ja"
    launchIntent.data = Uri.parse(url)

    val intentPick = Intent()
    intentPick.action = Intent.ACTION_PICK_ACTIVITY
    intentPick.putExtra(Intent.EXTRA_TITLE, "Launch using")
    intentPick.putExtra(Intent.EXTRA_INTENT, launchIntent)
    startActivityForResult(intentPick, 1)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 1) {
        val packageName = data?.component!!.packageName // パッケージ名取得方法①
        val appName = packageManager.getApplicationLabel(packageName, PackageManager.GET_META_DATA).toString()
        startActivity(data)
    }
}
Case2.kt
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val url = "https://www.google.com/?hl=ja"
    val intent = Intent(Intent.ACTION_VIEW)
    intent.data = Uri.parse(url)
    val pendingIntent = PendingIntent.getBroadcast(
            this,
            123,
            Intent(this, ShareBroadcastReceiver::class.java),
            PendingIntent.FLAG_UPDATE_CURRENT
    )

    val chooser = Intent.createChooser(intent, null, pendingIntent.intentSender)

    button.setOnClickListener {
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(chooser)
        }
    }
}

class ShareBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        val packageName2 = intent?.getParcelableExtra<ComponentName>(Intent.EXTRA_CHOSEN_COMPONENT)?.packageName // パッケージ名取得方法②
        val keys = Objects.requireNonNull(intent!!.extras)?.keySet()
        if (keys != null) {
            for (key in keys) {
                try {
                    val packageName = (intent!!.extras!![key] as ComponentName?)?.packageName// パッケージ名取得方法③
                    val packageManager: PackageManager? = context?.packageManager
                    val appName = packageManager?.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)) as String
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }
}

上記で取得できるパッケージ名例は以下

com.android.chrome
org.mozilla.firefox
jp.co.fenrir.android.sleipnir

取得できるラベル名は以下(※必ずしもアプリ名とは一致しません)

Chrome
Firefox
Sleipnir
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?