Android 安卓月刊中的重复报警
我在设置重复警报时遇到一些问题,该警报将在每个月或每两个月的特定日期(由用户输入)触发。 到目前为止,我正在使用服务进行通知,广播接收者以及待定的意图。我无法理解的是:Android 安卓月刊中的重复报警,android,android-alarms,Android,Android Alarms,我在设置重复警报时遇到一些问题,该警报将在每个月或每两个月的特定日期(由用户输入)触发。 到目前为止,我正在使用服务进行通知,广播接收者以及待定的意图。我无法理解的是: alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 30*month, pendingIntent); 我们如何在这里设置函数,这将如何影响电池寿命,是否还有其他事情(如将日期存
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 30*month, pendingIntent);
我们如何在这里设置函数,这将如何影响电池寿命,是否还有其他事情(如将日期存储在数据库中,仅在触发时调用它?)等。。
1.通知服务扩展服务
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);
}
二,。重复法
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是第一个代码扩展服务,如果您的设备关闭,警报将消失。我建议您使用alternet方式在长时间(如一个月)后触发操作。尝试提醒或日历事件
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);
}
请检查一下电话号码
请检查,如果您的设备关闭,警报将消失。我建议您使用alternet方式在长时间(如一个月)后触发操作。尝试提醒或日历事件 请检查一下电话号码
请检查您应该避免使用间隔30天的
设置重复
,原因有两个:
SetRepeating
不精确,其功能类似于setIncepactRepeating
,这意味着可能会将警报切换为整个间隔时间(在您的情况下最多30天)。如报告中所述: 您的闹钟的第一个触发器不会在请求的时间之前触发,但是 这可能不会在此后的几乎整个时间间隔内发生
设置(int、long、pendingent)
并在每个实例后计算下一个警报
您应该避免使用间隔30天的
SetRepeating
,原因有二:
SetRepeating
不精确,其功能类似于setIncepactRepeating
,这意味着可能会将警报切换为整个间隔时间(在您的情况下最多30天)。如报告中所述: 您的闹钟的第一个触发器不会在请求的时间之前触发,但是 这可能不会在此后的几乎整个时间间隔内发生
设置(int、long、pendingent)
并在每个实例后计算下一个警报
基本上
- 您可以将pendigIntent初始化为特定目的,该目的将接收警报并触发预期操作
- AlarmManager.INTERVAL_DAY*30*月-指定月数(1个月/2个月)*30*日间隔(即24小时)
- 您可以创建一个类似AlarmReceiver的类,该类将具有onReceive()方法,您可以在其中执行操作
- 您可以将pendigIntent初始化为特定目的,该目的将接收警报并触发预期操作
- AlarmManager.INTERVAL_DAY*30*月-指定月数(1个月/2个月)*30*日间隔(即24小时)
- 您可以创建一个类似AlarmReceiver的类,该类将具有onReceive()方法,您可以在其中执行操作
有关详细信息,请访问如果您的设备每月至少关闭一次,您不认为您的闹钟会关闭吗?@Kedarnath nope。我已经通过广播接收器在开机时找回了它,这很好,但我建议你使用其他方法,因为一个月是很长的一段时间。正是@Kedarnath。这种方法肯定会耗尽电池电量。我也会从你的方法中找到解决办法。但我希望这种方法也能奏效。所以,我在等待一些解释。如果你的设备每月至少关闭一次,你不认为你的闹钟会关闭吗?@Kedarnath nope。我已经通过广播接收器在开机时找回了它,这很好,但我建议你使用其他方法,因为一个月是很长的一段时间。正是@Kedarnath。这种方法肯定会耗尽电池电量。我也会从你的方法中找到解决办法。但我希望这种方法也能奏效。所以,我在等待一些解释。你是对的@Alex。我正在寻找替代品,因为它可能耗尽电池寿命兄弟。所以,我在想手机中的飞行模式。如果系统日期等于目标日期,则可能会关闭飞行模式并触发警报。。差不多吧。我不知道如何实现它。你当然是对的