You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 12下从BroadcastReceiver启动AlertActivity失败问题求助

解决方案

针对Android 12上从BroadcastReceiver无法启动AlertActivity且需锁屏显示的问题,核心是适配Android 12的后台Activity启动限制,以下是具体步骤:

1. 声明必要权限

在AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
  • POST_NOTIFICATIONS:Android 13+必须动态申请,用于发送通知
  • USE_FULL_SCREEN_INTENT:允许通过全屏意图启动Activity,无需动态申请,但需声明

2. 配置AlertActivity属性

在AndroidManifest.xml中修改AlertActivity的配置,确保能在锁屏显示且符合Android 12的组件要求:

<activity
    android:name=".AlertActivity"
    android:exported="true"
    android:launchMode="singleTop"
    android:showOnLockScreen="true"
    android:showWhenLocked="true"
    android:turnScreenOn="true"
    android:excludeFromRecents="true">
</activity>
  • android:exported="true":Android 12+要求可外部调用的组件必须显式声明
  • showOnLockScreen/showWhenLocked/turnScreenOn:实现锁屏显示并唤醒屏幕
  • singleTop:避免重复创建Activity实例

3. 调整BroadcastReceiver的启动逻辑

Android 12+禁止后台直接启动Activity,必须通过全屏通知意图触发。修改BroadcastReceiver的onReceive方法:

@Override
public void onReceive(Context context, Intent intent) {
    // 构建启动AlertActivity的Intent
    Intent activityIntent = new Intent(context, AlertActivity.class);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // 创建全屏PendingIntent(Android 12+需添加FLAG_IMMUTABLE)
    PendingIntent fullScreenIntent = PendingIntent.getActivity(
            context,
            0,
            activityIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );

    // 构建高优先级通知,绑定全屏意图
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "ALERT_CHANNEL_ID")
            .setSmallIcon(R.drawable.ic_alert)
            .setContentTitle("警报通知")
            .setContentText("触发警报提醒")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setFullScreenIntent(fullScreenIntent, true);

    // 发送通知
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(1, builder.build());
}

4. 创建通知渠道(Android 8.0+要求)

在Application或MainActivity的初始化代码中创建通知渠道:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(
            "ALERT_CHANNEL_ID",
            "警报渠道",
            NotificationManager.IMPORTANCE_HIGH
    );
    channel.setDescription("用于触发警报的通知渠道");
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

5. 动态申请POST_NOTIFICATIONS权限(Android 13+)

在MainActivity中添加权限申请逻辑:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            // 根据权限申请结果处理后续逻辑
        }).launch(Manifest.permission.POST_NOTIFICATIONS);
    }
}

内容的提问来源于stack exchange,提问作者VSirchenko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 16:37:01