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

使用AlarmManager时BroadcastReceiver的onReceive未触发问题

问题:AlarmManager触发的BroadcastReceiver的onReceive始终不执行

我的应用需要在指定间隔后发送通知,但使用AlarmManager时,BroadcastReceiver的onReceive方法始终未触发。已精简代码排查,尝试set、setExactAndAllowWhileIdle等方法均无效。

相关代码

NotificationService 代码

public void notification(Context context){
    NotificationChannel channel = new NotificationChannel("CHANNEL_1", "CHANNEL_NAME", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("CHANNEL_DESC");

    NotificationManagerCompat nm = NotificationManagerCompat.from(context);
    nm.createNotificationChannel(channel);

    AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        if(!alarmManager.canScheduleExactAlarms()){
            return;
        }
    }
    Intent alarmIntent = new Intent();
    alarmIntent.putExtra("titleExtra", "Title");
    alarmIntent.putExtra("textExtra", "Text");
    alarmIntent.setAction("TEST_ACTION");
    //context.sendBroadcast(alarmIntent);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    Toast.makeText(context, "Scheduled for 5 seconds", Toast.LENGTH_SHORT).show();
}

MyBroadcastReceiver 代码

@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("ON RECEIVE");

    String message = intent.getStringExtra("textExtra");
    String title = intent.getStringExtra("titleExtra");
    Notification notification;

    notification = new NotificationCompat.Builder(context, "CHANNEL_1")
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.icon)
            .setWhen(System.currentTimeMillis())
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentTitle(title)
            .setContentText(message)
            .build();

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(1234, notification);
}

AndroidManifest.xml 代码

<receiver
    android:name=".services.NotificationService"
    android:exported="true"
    android:enabled="true">
    <intent-filter>
        <action android:name="TEST_ACTION" />
    </intent-filter>
</receiver>

关键排查信息

  • 取消注释context.sendBroadcast(alarmIntent)时,onReceive会触发且通知正常发送,说明onReceive逻辑和通知逻辑无问题。
  • 若将Intent改为Intent alarmIntent = new Intent(context, MainMenuActivity.class);,sendBroadcast也失效,暂不确定是否相关。
  • 已尝试动态注册Receiver、调整enabled/exported属性,均无改善。

解决方案

核心问题是Manifest里注册的Receiver类名完全错误:

你在Manifest中注册的是.services.NotificationService,但实际处理广播的类是MyBroadcastReceiver,这直接导致AlarmManager触发的广播找不到对应的处理类,自然不会执行onReceive。

修复步骤:

  1. 修改AndroidManifest.xml的receiver节点,将android:name改为MyBroadcastReceiver的正确类路径:
    比如如果MyBroadcastReceiver在根包下,就写.MyBroadcastReceiver;如果在services包下,就写.services.MyBroadcastReceiver:
<receiver
    android:name=".MyBroadcastReceiver"
    android:exported="true"
    android:enabled="true">
    <intent-filter>
        <action android:name="TEST_ACTION" />
    </intent-filter>
</receiver>
  1. 针对Android 12及以上系统,额外检查两项:

    • 确保canScheduleExactAlarms权限已获取(代码中已做判断,可补充引导用户跳转设置申请的逻辑);
    • 进入应用设置→电池→后台活动,允许应用在后台运行,避免系统限制AlarmManager触发。
  2. 若使用动态注册Receiver,需确保注册逻辑在应用启动时执行(比如Activity的onCreate中),且IntentFilter包含TEST_ACTION:

MyBroadcastReceiver receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter("TEST_ACTION");
registerReceiver(receiver, filter);

修改后,AlarmManager到时间即可正确触发MyBroadcastReceiver的onReceive方法。


内容的提问来源于stack exchange,提问作者Oskar Donaldson-Holte

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 15:43:13