如何检查后台是否允许startActivity(Android)?

问题描述 投票:0回答:1

收到通知后,由于 Android 中的后台限制,Activity 无法启动

如何检查我是否可以通过代码调用startActivity而无需try catch块 https://developer.android.com/guide/components/activities/background-starts

android start-activity
1个回答
0
投票

使用包管理器

Intent intent = new Intent(this, YourTargetActivity.class);

// Check if there is at least one activity that can handle the intent packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);

if (!activities.isEmpty()) {
    // There's at least one activity that can handle this intent, so it's safe to start it
    startActivity(intent);
} else {
    // No activity can handle the intent, handle this case as needed (e.g., show an error message)
}
© www.soinside.com 2019 - 2024. All rights reserved.