Androidはパッケージ名でアプリケーションを起動します
4394 ワード
パッケージ名によるアプリケーションの開始
/**
*
* , activity
* MainActivity
*/
fun launchApp(context: Context, packageName: String) {
var mainActivity: String? = null
val packageManager = packageManager
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED or Intent.FLAG_ACTIVITY_NEW_TASK)
@SuppressLint("WrongConstant")val activities =
packageManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES)
for (info in activities) {
if (info.activityInfo.packageName.equals(packageName)) {
mainActivity = info.activityInfo.name
break
}
}
if (mainActivity.isNullOrEmpty()) {
return
}
intent.setComponent(ComponentName(packageName, mainActivity))
startActivity(intent)
}