无法启动外部应用程序
Can't start external application
我有以下代码来启动外部应用程序,但它不起作用:
private fun launchApplication(packageName: String)
{
try
{
val application = packageManager.getLaunchIntentForPackage(packageName)
startActivity(application)
}
catch (ex: Exception) { }
}
我试过以下软件包:
- com.google.android.youtube
- com.teamviewer.quicksupport.market
有什么想法吗?
编辑
使用来自@Mayur Gajra 的代码,它与 com.google.android.youtube
一起工作,但不与 com.teamviewer.quicksupport.market
一起工作,抛出以下错误:
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 }
但是如果我搜索使用以下代码安装的软件包,TeamViewer 会出现:
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}
- packageName = com.teamviewer.quicksupport.market
- 类名=com.teamviewer.quicksupport.QSApplication
有什么想法吗?
您可以尝试这种方式并让系统找到 activity 和 Launcher
类别的那个包并启动它。
private fun launchApplication(packageName: String) {
try {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.setPackage(packageName)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
} catch (ex: Exception) {
}
}
最终通过以下代码解决:
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) { }
}
一些补充说明:如果申请在Lock Task Mode,此方法将不起作用,记得之前调用stopLockTask()
。
我有以下代码来启动外部应用程序,但它不起作用:
private fun launchApplication(packageName: String)
{
try
{
val application = packageManager.getLaunchIntentForPackage(packageName)
startActivity(application)
}
catch (ex: Exception) { }
}
我试过以下软件包:
- com.google.android.youtube
- com.teamviewer.quicksupport.market
有什么想法吗?
编辑
使用来自@Mayur Gajra 的代码,它与 com.google.android.youtube
一起工作,但不与 com.teamviewer.quicksupport.market
一起工作,抛出以下错误:
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 }
但是如果我搜索使用以下代码安装的软件包,TeamViewer 会出现:
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}
- packageName = com.teamviewer.quicksupport.market
- 类名=com.teamviewer.quicksupport.QSApplication
有什么想法吗?
您可以尝试这种方式并让系统找到 activity 和 Launcher
类别的那个包并启动它。
private fun launchApplication(packageName: String) {
try {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.setPackage(packageName)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)
} catch (ex: Exception) {
}
}
最终通过以下代码解决:
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) { }
}
一些补充说明:如果申请在Lock Task Mode,此方法将不起作用,记得之前调用stopLockTask()
。