gpt4 book ai didi

android - Android Monthly 中的重复闹钟

转载 作者:行者123 更新时间:2023-11-29 15:12:44 24 4
gpt4 key购买 nike

我在设置重复警报时遇到了一些问题,该警报应在每个月或每两个月的特定日期(由用户输入)触发。到目前为止,我正在使用通知服务、BroadcastReceiver 以及未决的 Intent。我无法理解的是:

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 30*month, pendingIntent);

我们如何在这里设置函数,这将如何影响电池生命周期,是否还有其他事情(例如将日期存储在数据库中并且仅在触发时调用它?)等等。1.Notification Service 扩展服务

public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Getting Notification Service
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
/*
* When the user taps the notification we have to show the Home Screen
* of our App, this job can be done with the help of the following
* Intent.
*/
Intent intent1 = new Intent(this.getApplicationContext(), com.expandablelistItems.demo.adapter.DynamicActivity.class);

Notification notification = new Notification(R.drawable.ic_launcher,
"Payment of your demoReminder", System.currentTimeMillis());

intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.setLatestEventInfo(this.getApplicationContext(),
"demo", "Payment of your demoReminder",
pendingNotificationIntent);

mManager.notify(0, notification);
}

2。重复的方法

if  (current_Month == Calendar.FEBRUARY){//for feburary month)
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
if(cal.isLeapYear(calendar.get(Calendar.YEAR))){//for leap year feburary month
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 29*month, pendingIntent);
Toast.makeText(getActivity(), "februry", Toast.LENGTH_SHORT).show();}
else{ //for non leap year feburary month
Toast.makeText(getActivity(), "feb", Toast.LENGTH_SHORT).show();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 28*month, pendingIntent);
}
}

这是广播接收器

 @Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
// We're invoking a service in this method which shows Notification to the User
Intent myIntent = new Intent(context, NotificationService.class);
context.startService(myIntent);
}

其中notificationService是第一个代码扩展服务

最佳答案

您应该避免使用间隔为 30 天的 SetRepeating,原因有二:

<醇>
  • 它不会在每个月的同一天重复(也可能会跳过一个月)。
  • 自 API 19 以来,SetRepeating 是不精确的,其功能与 SetInexactRepeating 类似,这意味着可能会在整个间隔持续时间内移动警报(在您的情况下最多 30 天) .
    docs 中所述:

    Your alarm's first trigger will not be before the requested time, but it might not occur for almost a full interval after that time

  • 只要您希望警报在确切的日期触发,您应该使用 set(int, long, PendingIntent) 并在每次实例后计算下一个警报。

    关于android - Android Monthly 中的重复闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28850075/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com