仅当我处于设置警报的活动中时警报才会触发

发布于 2024-10-15 04:27:31 字数 1618 浏览 3 评论 0原文

我正在通过 ItemEdit Activity 在我的应用中创建警报。人们可以在其中编辑/查看其笔记/待办事项,还可以为该项目设置提醒/闹钟。我使用以下代码设置闹钟:

private void createAlarm() {
     Intent intent = new Intent(this, ReminderReceiver.class);
     intent.putExtra("reminder_message", "Reminder Received!");
     intent.putExtra("item_id", mRowId);
     PendingIntent sender = 
         PendingIntent.getBroadcast(
                 getApplicationContext(), 
                 ALARM_ID, 
                 intent, 
                 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

     // Get the AlarmManager service
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
     // Set alarm to the time given by the user.
     am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}

这是接收器

    public class ReminderReceiver extends BroadcastReceiver {

    private static final String TAG = "MyApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("reminder_message");
            Log.v(TAG, message);
        } catch(Exception e) {
            Log.v(TAG, "OH SNAP!");
            e.printStackTrace();
        }

    }

编辑:我的清单中还有以下内容:

< /receiver>

如果我停留在设置闹钟的 Activity 中,则接收效果良好。如果我点击后退按钮返回到列出所有项目的 ListActivity 或完全离开应用程序,则警报永远不会触发。我在设置闹钟时是否做错了什么,它只能从设置它的活动中触发?

谢谢。

I'm creating an alarm in my app from the ItemEdit Activity. Its where one can edit/view their note/todo item, they can also set a reminder/alarm for the item there. I set the alarm with the following code:

private void createAlarm() {
     Intent intent = new Intent(this, ReminderReceiver.class);
     intent.putExtra("reminder_message", "Reminder Received!");
     intent.putExtra("item_id", mRowId);
     PendingIntent sender = 
         PendingIntent.getBroadcast(
                 getApplicationContext(), 
                 ALARM_ID, 
                 intent, 
                 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

     // Get the AlarmManager service
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
     // Set alarm to the time given by the user.
     am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}

And here is the Receiver

    public class ReminderReceiver extends BroadcastReceiver {

    private static final String TAG = "MyApp";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String message = bundle.getString("reminder_message");
            Log.v(TAG, message);
        } catch(Exception e) {
            Log.v(TAG, "OH SNAP!");
            e.printStackTrace();
        }

    }

Edit: Also I have the following in my manifest:

<receiver android:process=":remote" android:name="ReminderReceiver"></receiver>

If I stay in the Activity where I set the alarm it is received fine. If I hit the back button to return to my ListActivity where all the items are listed or leave the app entirely the alarm never triggers. Have I done something wrong in setting up my alarm that it only triggers from the Activity that set it?

Thanks.

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

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

发布评论

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

评论(1

怪我入戏太深 2024-10-22 04:27:31

您需要查看 Service 而不是 Activity< /code> 用于不与用户交互的长期进程(如闹钟)。

You need to look into a Service instead of an Activity for a long-living process that doesn't interact with the user (like an alarm clock).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文