I've got the following code to start external applications but it's not working:
private fun launchApplication(packageName: String)
{
try
{
val application = packageManager.getLaunchIntentForPackage(packageName)
startActivity(application)
}
catch (ex: Exception) { }
}
I've tried with the following packages:
Any ideas?
EDIT
With the code from @Mayur Gajra it is working with com.google.android.youtube
but not with com.teamviewer.quicksupport.market
, throwing the following error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x4000000 pkg=com.teamviewer.quicksupport.market }
But if i search packages installed with the following code, TeamViewer shows up:
val intent = Intent(Intent.ACTION_MAIN, null)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
val appsList = packageManager.queryIntentActivities(intent, 0)
ResolveInfo{40c33b1 com.teamviewer.quicksupport.market/com.teamviewer.quicksupport.ui.QSActivity m=0x108000}
Any ideas?
Finally resolved with the following code:
private fun launchApplication(packageName: String)
{
try
{
// Find application with that package name
val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
val pkgAppsList = packageManager.queryIntentActivities(mainIntent, 0)
val application = pkgAppsList.first { it.activityInfo.applicationInfo.packageName == packageName }
// Start that application
val intent = Intent()
intent.setClassName(application.activityInfo.packageName, application.activityInfo.name)
startActivity(intent)
}
catch (ex: Exception) { }
}
Some aditional comments: this method won't work if application is in Lock Task Mode, remember to call stopLockTask()
before.