我创建了一个用于在特定日期和时间显示通知的类。这是用于创建通知的类的源代码:
public class TimeAlarm extends BroadcastReceiver {
public static NotificationManager mNotificationManager;
public static int SIMPLE_NOTFICATION_ID = 0;
@Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Scheduled Training";
CharSequence message = "Please attend the scheduled training";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, TextToSpeechScreen.class), 0);
//OLD CODE
Notification notifyDetails = new Notification(R.drawable.ic_launcher,
"Barclays Scheduled Training", System.currentTimeMillis());
notifyDetails.setLatestEventInfo(context, from, message, contentIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notifyDetails.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
// notifyDetails.defaults |= Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
}
这是我从活动中调用以在特定时间设置通知的方法:
// setting alarm
public void setOneTimeAlarm() {
// the time set
Calendar calendar = GregorianCalendar.getInstance();
// calendar.setTime(model.getStartDate());
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, model.getStartTimeHour());
// calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, model.getStartTimeMinutes());
// calendar.set(Calendar.MINUTE, 53);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, count);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
}
一切正常,现在我需要在TimeAlarm类中实现服务,以便可以从后台进程触发通知。我应该如何实现这个?是否可以使用后台进程发送通知?提前致谢!