我想在特定时间发送通知。以下是我的代码。现在,我只在启动应用程序时收到通知,但是我没有在指定时间收到通知。你能帮我理解我做错了吗?
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());
感谢您的帮助!
答案 0 :(得分:1)
您需要添加:
@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方法中显示通知。
请查看here它是否完整实现了警报类。