skip to Main Content

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.

2

Answers


  1. try this

    try {
     launchIntent =packageManager.getLaunchIntentForPackage("com.android.chrome")
     startActivity(launchIntent)
    } catch (E: Exception) {
      println("Package not found")
    }
    
    
    
    
    
    Login or Signup to reply.
  2. 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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search