首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin : packageManager.getLaunchIntentForPackage()不能启动应用程序

Kotlin : packageManager.getLaunchIntentForPackage()不能启动应用程序
EN

Stack Overflow用户
提问于 2022-02-05 12:03:20
回答 1查看 830关注 0票数 0

我有这样的kotlin应用程序:

代码语言:javascript
复制
package com.test.openchrome

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val launcher = findViewById<Button>(R.id.openchrome)
        launcher.setOnClickListener{
            var launchIntent: Intent? = null
            try {
                launchIntent = packageManager.getLaunchIntentForPackage("com.android.chrome")
            } catch (ignored: Exception) {
            }

            if (launchIntent == null) {
                startActivity(Intent(Intent.ACTION_VIEW).setData(Uri.parse("https://play.google.com/store/apps/details?id=" + "com.android.chrome")))
            } else {
                startActivity(launchIntent)
            }
        }
    }
}

我已经在我的android上安装了铬。

但是当我按下“打开铬”按钮时,铬就不会打开了。相反,切换到playstore。

EN

回答 1

Stack Overflow用户

发布于 2022-09-06 15:57:05

尝尝这个

代码语言:javascript
复制
try {
 launchIntent =packageManager.getLaunchIntentForPackage("com.android.chrome")
 startActivity(launchIntent)
} catch (E: Exception) {
  println("Package not found")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70997828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档