[英]Show status bar notification in certain time
I want to show the user notifications everyday in the same time. 我想每天同时显示用户通知。 For now I only have this code: 现在,我只有以下代码:
My TimeAlarm.class that shows the notifications: 我的TimeAlarm.class显示通知:
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Nithin";
CharSequence message = "Crazy About Android...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.test);
// r.play();
nm.notify(1, notif);
}
}
And initialization of the notifications to show every 5 seconds: 通知的初始化以每5秒显示一次:
public void setRepeatingAlarm() {
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(5 * 1000), pendingIntent);
}
Now my question is how should I modify this code to set the notification to repeat every day in certain time. 现在我的问题是我应该如何修改此代码以将通知设置为在特定时间每天重复。 Example every day in 14:36 o'clock? 例如每天14:36钟吗?
The easy-but-inexact way is to change System.currentTimeMillis()
to a calculation of 14:36 tomorrow, probably using a Calendar
object: 一种简单但不精确的方法是明天将System.currentTimeMillis()
更改为14:36的计算,可能使用Calendar
对象:
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 14);
cal.set(Calendar.MINUTE, 36);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if (cal.getTimeInMillis() < System.currentTimeMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
I say "easy-but-inexact" because Android 4.4 has changed the behavior of setRepeating()
to be inexact, once your android:targetSdkVersion
is 19 or higher. 我说“ easy-but-inexact”是因为一旦您的android:targetSdkVersion
为19或更高版本,Android 4.4就会将setRepeating()
的行为更改为不精确。 If you need exact alarms, you will need to use setExact()
and do your own "repeating" (by calling setExact()
again when the alarm goes off). 如果您需要精确的警报,则需要使用setExact()
并进行自己的“重复”(通过在警报消失时再次调用setExact()
)。 You would still use the same Calendar
sort of calculation, just on every setExact()
call. 您仍然会在每次setExact()
调用上使用相同的Calendar
类型的计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.