特定工作日的Android通知直接关闭 [英] Android Notification on specific weekday goes off directly
本文介绍了特定工作日的Android通知直接关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过使用以下代码来通知在周四和周五关闭:这段代码是星期四的
I'm trying to make a notification to go off on Thursdays and Fridays by using the following code: This code is for thursdays
Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, 14);
updateTime.set(Calendar.MINUTE, 44);
updateTime.set(Calendar.SECOND, 0);
updateTime.set(Calendar.DAY_OF_WEEK, 5);
if(updateTime.before(new GregorianCalendar())){
updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
Intent intent = new Intent(context, Alarm4.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), pi);
但是,如果时间已经超过通知时间,则此代码将无法直接弹出通知.
However, this code does not work the notification pops-up directly if the time did already past the notification time.
我想念什么?
推荐答案
这样做.
if (updateTime.getTimeInMillis() < System.currentTimeMillis()
|| updateTime.before(new GregorianCalendar())) {
updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24);
updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
并创建一个类来设置警报".
and make a class to set Alarm.
public class setAlarm {
Context context;
public setAlarm(Context c) {
// TODO Auto-generated constructor stub
this.context = c;
}
public void settingAlarm(Calendar calendar, int counter) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
counter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
,并且警报BroadCastReceiver是公共类AlarmReceiver扩展了BroadcastReceiver {
and Alarm BroadCastReceiver is public class AlarmReceiver extends BroadcastReceiver {
private NotificationManager manager;
private int NOTIFICATION_ID;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Food Time", System.currentTimeMillis());
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "FoodTime",
"Click to View your food", pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = Uri
.parse("android.resource://"
+ "com.technogalax.sixsmallmealaday.alarm" + "/"
+ R.raw.nsound);
manager.notify(NOTIFICATION_ID, notification);
}
}
这篇关于特定工作日的Android通知直接关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文