如何在特定时间在 android 应用程序中设置通知?
How do I set notification in android application at specific time?
我在 android application
到 set up Notification(Alarm)
中使用了以下代码 AlarmManager
。
public void setAlarm() {
Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(40000), pendingIntent);
}
或者我应该使用下面的代码
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 3);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 21);
calendar.set(Calendar.HOUR_OF_DAY,11);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
I want to set (notification)alarm at this specific time : 11:15AM on 21-March-2015
Can any one help me to set this?
- 将警报管理器与服务一起使用。
- 服务将在清单文件中引入。
- 请认真阅读以下教程。
一个。 http://nnish.com/2014/12/16/scheduled-notifications-in-android-using-alarm-manager/
乙。 http://androidideasblog.blogspot.co.uk/2011/07/alarmmanager-and-notificationmanager.html
C。 https://github.com/benbahrenburg/benCoding.AlarmManager
D.http://www.banane.com/2014/05/07/simple-example-of-scheduled-self-clearing-android-notifications/
希望对你有所帮助
首先,您需要使用 Calendar
来设置您的日期,例如:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 3);
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.DAY_OF_MONTH, 15);
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 15);
然后在您的 setAlarm()
方法中您只需设置一次闹钟。你不需要重复它:
public void setAlarm() {
Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
}
运行每秒一个线程,将您指定的时间与当前时间进行比较,如果匹配则设置通知。
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm aa");
String dateTimeinSrting = "21-Mar-2015 11:15 am";
try {
Date dateTime = formatter.parse(dateTimeInString);
} catch (ParseException e) {
e.printStackTrace();
}
float dateTimeInMillis = dateTime.getTime();
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
Calendar calendar;
if(Float.compare(dateTimeInMillis, calendar.getTimeInMillis())){
//Set the notification here
}
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
我在 android application
到 set up Notification(Alarm)
中使用了以下代码 AlarmManager
。
public void setAlarm() {
Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(40000), pendingIntent);
}
或者我应该使用下面的代码
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 3);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 21);
calendar.set(Calendar.HOUR_OF_DAY,11);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
I want to set (notification)alarm at this specific time : 11:15AM on 21-March-2015
Can any one help me to set this?
- 将警报管理器与服务一起使用。
- 服务将在清单文件中引入。
- 请认真阅读以下教程。
一个。 http://nnish.com/2014/12/16/scheduled-notifications-in-android-using-alarm-manager/
乙。 http://androidideasblog.blogspot.co.uk/2011/07/alarmmanager-and-notificationmanager.html
C。 https://github.com/benbahrenburg/benCoding.AlarmManager D.http://www.banane.com/2014/05/07/simple-example-of-scheduled-self-clearing-android-notifications/
希望对你有所帮助
首先,您需要使用 Calendar
来设置您的日期,例如:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 3);
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.DAY_OF_MONTH, 15);
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 15);
然后在您的 setAlarm()
方法中您只需设置一次闹钟。你不需要重复它:
public void setAlarm() {
Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
}
运行每秒一个线程,将您指定的时间与当前时间进行比较,如果匹配则设置通知。
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm aa");
String dateTimeinSrting = "21-Mar-2015 11:15 am";
try {
Date dateTime = formatter.parse(dateTimeInString);
} catch (ParseException e) {
e.printStackTrace();
}
float dateTimeInMillis = dateTime.getTime();
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
Calendar calendar;
if(Float.compare(dateTimeInMillis, calendar.getTimeInMillis())){
//Set the notification here
}
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);