AlarmReceiver 中的 Android 通知

发布于 2025-01-06 21:28:06 字数 1526 浏览 2 评论 0原文

我希望能够创建一个警报,每 2 分钟向用户发出一次启动应用程序的通知。一切正常,但当我手动启动应用程序时会出现通知。这是我的代码:

接收器:

public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "myactivity";
    CharSequence message = "click to start activity";


    Intent scheduledIntent = new Intent(context, AmslerTestActivity.class);


    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            scheduledIntent, 0);
    Notification notif = new Notification(R.drawable.icon,
            message, System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    notif.defaults |= Notification.DEFAULT_SOUND;
    nm.notify(1, notif);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(scheduledIntent);

}

调度程序:

cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 01);
    cal.set(Calendar.SECOND, 0);

    Intent intent = new Intent(myactivity.this, AlarmReceiver.class);

    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    // Get the AlarmManager service
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender);

我希望通知仅在代码未运行时响起。

I want to be able to create an alarm to ring a notification to the user to start the application every 2 minutes. Everything works fine, but the notification appears when i start my application manually. Here are my code :

Receiver:

public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "myactivity";
    CharSequence message = "click to start activity";


    Intent scheduledIntent = new Intent(context, AmslerTestActivity.class);


    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            scheduledIntent, 0);
    Notification notif = new Notification(R.drawable.icon,
            message, System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    notif.defaults |= Notification.DEFAULT_SOUND;
    nm.notify(1, notif);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(scheduledIntent);

}

Scheduler:

cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 01);
    cal.set(Calendar.SECOND, 0);

    Intent intent = new Intent(myactivity.this, AlarmReceiver.class);

    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    // Get the AlarmManager service
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender);

I want the notification to only ring when the code is not running.

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

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

发布评论

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

评论(1

北陌 2025-01-13 21:28:06

将代码的“调度程序”部分放入 onPauseonStoponDestroy 中,以便仅在 Activity< 一次时设置闹钟/code> 停止。请阅读此处来决定哪个最适合您的目的。然后,在执行am.setRepeating时,对triggerAtTime使用System.currentTimeMillis() + 2 * 60 * 1000,使闹钟触发2从 Activity 停止的时间算起的分钟数。然后,它将根据第三个参数“间隔”每 2 分钟重复一次,您当前已将其正确设置为 2 分钟。

Put the "scheduler" part of your code in onPause, onStop or onDestroy in order to set the alarm only once your Activity stops. Read here to decide which is best for your purposes. Then, when doing am.setRepeating, use System.currentTimeMillis() + 2 * 60 * 1000 for the triggerAtTime to make the alarm trigger 2 min from the time the Activity stops. Then it will repeat every 2 min based on the 3rd parameter, the interval, which you currently have set correctly to 2 min.

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