0

I am developing an Android app that, among other things, launches other apps on the device. Here is the code that I use when I would like to launch an external app:

    fun openApp(context: Context, appName: String, packageName: String) {
        if (isAppInstalled(context, packageName))
            if (isAppEnabled(context, packageName)) {
                context.startActivity(context.packageManager.getLaunchIntentForPackage(packageName))
            }
            else
                Toast.makeText(context, "$appName app is not enabled.", Toast.LENGTH_SHORT).show()
        else
            Toast.makeText(context, "$appName app is not installed.", Toast.LENGTH_SHORT).show()
    }

    private fun isAppInstalled(context: Context, packageName: String): Boolean {
        val pm = context.packageManager
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
            return true
        } catch (ignored: PackageManager.NameNotFoundException) {
        }

        return false
    }

    private fun isAppEnabled(context: Context, packageName: String): Boolean {
        var appStatus = false
        try {
            val ai = context.packageManager.getApplicationInfo(packageName, 0)
            if (ai != null) {
                appStatus = ai.enabled
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        return appStatus
    }
}

Certain apps will be launched without a problem and others (e.g. Google Play Music) will cause the following error:

Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference

I have found some other discussion related to this issue here and here which suggest it has to do with Google Play Services but was apparently fixed many versions ago (all apps are update to date). Does anyone of any insight as to what would make some apps launch properly and not others?

Edit: As I linked above, I am aware of the fixes that have been discussed previously but all of my apps (including Google Play services at version 12.8.74) are update so those fixes do not help.

Matti Granovsky
  • 445
  • 1
  • 7
  • 11

0 Answers0