0

I have kotlin apps program like this:

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)
            }
        }
    }
}

I have installed chrome on my android.
but when i press "open chrome" button, chrome doesn't open. instead switch to playstore.

Fikran
  • 5
  • 3
  • On Android 11 and later you need to explicitly have `queries` elements in your manifest for packagemanager to return anything. https://stackoverflow.com/questions/30446052/getlaunchintentforpackage-is-null-for-some-apps – laalto Feb 05 '22 at 12:42

2 Answers2

0

try this

try {
 launchIntent =packageManager.getLaunchIntentForPackage("com.android.chrome")
 startActivity(launchIntent)
} catch (E: Exception) {
  println("Package not found")
}




0

You need to go AndroidManifest.xml and add

<queries>
    <package android:name="com.android.chrome" />

</queries>

After that in your code:

var launchIntent = packageManager.getLaunchIntentForPackage("com.android.chrome")
            startActivity(launchIntent)

and its done!

helvete
  • 2,455
  • 13
  • 33
  • 37