广播接收器未接收触发警报

发布于 2024-12-08 11:06:48 字数 1604 浏览 0 评论 0原文

我有以下问题。我有2节课。一个称为 AlarmService,另一个称为 TimeAlarm,它扩展了 BroadcastReceiver。 该应用程序应该执行以下操作:它应该在首选项中指定的时间生成一个新的警报(它已经这样做了...)也在 Logcat 中我可以看到警报是如何触发的。但问题是,应该显示的通知没有显示在状态栏中。

以下是我为此所拥有的所有代码:

AndroidManifest.xml:

<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

AlarmService.java:

private Context context;
private PendingIntent mAlarmSender;

public AlarmService(Context context) {
    this.context = context;
    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(context, myActivity.class);
    mAlarmSender = PendingIntent.getBroadcast(context, 0, notifyIntent, 0);
}

public void startAlarm(int stunde, int minute) {
    Calendar updateTime = Calendar.getInstance();

    updateTime.set(Calendar.HOUR_OF_DAY, stunde);
    updateTime.set(Calendar.MINUTE, minute);
    updateTime.set(Calendar.SECOND, 00);

    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, mAlarmSender);
}

TimeAlarm.java:

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("TEST", "onReceive() called...");
}

清单中的接收器位于标签内部,因此这应该不是问题。 问题是,如果我重新启动设备,它就会被调用。但如果警报被触发则不会。 但是如果触发警报,也应该调用 onReceive() 方法,不是吗?

感谢您的帮助!

I have the following problem. I have 2 classes. 1 is called AlarmService and the other is called TimeAlarm which extends BroadcastReceiver.
The App should do the following thing: It should generate a new Alarm to a time specified in the preferences (Which it already does...) also in Logcat i can see how the Alarm gets triggered. But the problem is, that the Notification which should be shown does not show up in the StatusBar.

Here is all the code i have for this:

AndroidManifest.xml:

<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

AlarmService.java:

private Context context;
private PendingIntent mAlarmSender;

public AlarmService(Context context) {
    this.context = context;
    Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
    notifyIntent.setClass(context, myActivity.class);
    mAlarmSender = PendingIntent.getBroadcast(context, 0, notifyIntent, 0);
}

public void startAlarm(int stunde, int minute) {
    Calendar updateTime = Calendar.getInstance();

    updateTime.set(Calendar.HOUR_OF_DAY, stunde);
    updateTime.set(Calendar.MINUTE, minute);
    updateTime.set(Calendar.SECOND, 00);

    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, mAlarmSender);
}

TimeAlarm.java:

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("TEST", "onReceive() called...");
}

The Receiver in the Manifest is inside of the Tag so that should not be a problem.
The problem is, that if i restart my device, it gets called. But not if an Alarm gets triggered.
But the onReceive() method should also be called if an alarm gets triggered, shouldn't it?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2024-12-15 11:06:48

您的意图过滤器仅侦听启动完整意图,而不是您自己的警报广播意图操作。更新您的意图过滤器,以便您的广播意图也被接收(这意味着对于您的特殊情况,将 Intent.ACTION_MAIN 的操作添加到您的意图过滤器中)

<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</receiver>

your intent filter is only listening to boot complete intents and not your own alarm broadcast intent action. update your intent filter so that your broadcast intent is also received (that means for your special case add the action of Intent.ACTION_MAIN to your intent filter)

<receiver android:name="com.ikalma.alarmmanager.TimeAlarm">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</receiver>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文