[英]How to send a Notification at a specific time?
我想在特定時間發送通知。 下面是我的代碼。 目前,我僅在啟動應用程序時收到通知,但在指定時間沒有收到通知。 你能幫我了解我做錯了什么嗎?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Trainieren sie jetzt!")
.setAutoCancel(true);
Intent resultIntent = new Intent(this, MainActivity2.class);
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 54);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, resultPendingIntent);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
感謝您的幫助!
您需要添加:
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(MainActivity.con, "Hello from alarm", 1).show();
// System.out.println("Hello from Alarm");
context.startService(new Intent(context, NotificationService.class));
}
在上面的示例中,我將流程定向到我的Service類以處理我的通知,管理喚醒鎖和后台服務實現。 如果您未實現服務,則可以僅在onRecieve方法中顯示通知。
請在此處查看它是否具有警報類的完整實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.