Kotlinapps是这样的:
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上安装了Chrome。
但是当我按下“打开chrome”按钮时,chrome没有打开。而是切换到playstore。
2条答案
按热度按时间5q4ezhmt1#
试试这个
avwztpqn2#
您需要转到AndroidManifest.xml并添加
在你的代码中,
var launchIntent = packageManager.getLaunchIntentForPackage(“com.android.chrome“)startActivity(launchIntent)
就这样!