bagashyt / launch_external_app

Launch external app from current application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

launch_external_app

Launch external app from current application

Setup

make function intent to launch external App

// function launch intent to package name app
    private fun launchApp(appPackageName: String) {

        if (isAppInstalled(appPackageName)) {
            val intent: Intent = this.packageManager?.
            getLaunchIntentForPackage(appPackageName)!!
            startActivity(intent)
        } else {
            try {
                startActivity(
                    Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("market://details?id=$appPackageName")
                    )
                )
            }
            // catch if application doesn't installed then open application page on playstore
            catch (exception: ActivityNotFoundException) {
                startActivity(
                    Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")
                    )
                )
            }
        }
    }

create function to check if application is installed or not

    private fun isAppInstalled(packageName: String): Boolean {
        return try {
            val packageManager = packageManager
            packageManager.getPackageInfo(packageName, 0)
            true
        } catch (e: PackageManager.NameNotFoundException) {
            false
        }
    }

How to use

change appPackageName with package name destination app

  private var appPackageName = "com.google.android.youtube"

Reference

About

Launch external app from current application


Languages

Language:Kotlin 88.2%Language:PureBasic 11.8%