通知无法在给定时间显示。我有字符串格式日期(“2014-1-10 17:43:00”)。我将其更改为日历并调用通知方法。但它没有用。请指导我,我需要什么?这是我的代码。
是字符串格式日期和调用方法。
String dtStart = "2014-1-10 17:43:00 ";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Calendar tdy1= Calendar.getInstance();
try {
d1 = format.parse(dtStart);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tdy1.setTime(d1);
long startTime = tdy1.getTimeInMillis();
Log.i("tdy1 milli",startTime+"");
createScheduleNotificationg(tdy1);
这是通知()。
public void createScheduleNotificationg(Calendar showCalendar){
int year = showCalendar.get(Calendar.YEAR);
int month = showCalendar.get(Calendar.MONTH); // Note: zero based!
int day = showCalendar.get(Calendar.DAY_OF_MONTH);
int hour = showCalendar.get(Calendar.HOUR_OF_DAY);
int minute = showCalendar.get(Calendar.MINUTE);
int second = showCalendar.get(Calendar.SECOND);
month+=1;
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
int id=(int)System.currentTimeMillis();
Intent intent = new Intent(this,timealarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, showCalendar.getTimeInMillis(), pendingIntent);
}
这是onReceive()
public void onReceive(Context context, Intent paramIntent) {
// TODO Auto-generated method stub
NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("http://www.papers.ch"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification
Notification notification = new Notification(R.drawable.ic_launcher, "Visit our homepage", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Visit our homepage", "http://www.papers.ch",pendingIntent);
// Fire the notification
notificationManager.notify(1, notification);
}