2012-01-30 82 views
5

我想在特定的日期和時間在android中設置我的通知,我正在通過在java中使用日期來嘗試它,但是我的通知在時間之前被觸發。所以我可以在指定的時間內讓它開火。提前致謝!如何在自定義時間開始通知?

這裏是我的通知代碼:

Calendar cal = java.util.Calendar.getInstance(); 
cal.set(2012, 00, 30); 
Date date = cal.getTime(); 
date.setHours(17); 
date.setMinutes(30); 
date.setSeconds(15); 

long time = date.getTime(); 

Log.e("date is",""+date); 
long when = time; 


Notification notification = new Notification(notificationIcon,tickerText,date.getTime()); 

notification.when = date.getTime(); 

RemoteViews contentView = new RemoteViews("com.LayoutDemo",R.layout.userscreen);   
notification.contentView= contentView; 
Intent intent = this.getIntent(); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 
notification.contentIntent= contentIntent; 

notification.flags= Notification.FLAG_AUTO_CANCEL; 

int NOTIFICATION_ID =1;    
nManager.notify(NOTIFICATION_ID,notification); 
+0

好,謝謝!待定你有關於如何開始播種通知服務的任何想法嗎? – 2012-01-30 12:29:24

回答

2

採用Android報警服務,並在該組的意圖

Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class); 
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0); 
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.add(Calendar.SECOND, 10); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);