2012-01-30 31 views
5

Tôi muốn đặt thông báo của mình trong android vào ngày và giờ cụ thể, tôi đang cố gắng thông báo bằng cách sử dụng ngày trong java, nhưng thông báo của tôi được kích hoạt trước thời gian. vì vậy tôi có thể làm gì để kích hoạt nó vào thời gian được chỉ định. Cảm ơn trước!Làm cách nào để bắt đầu thông báo vào thời gian tùy chỉnh?

Đây là mã của tôi để thông báo:

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

Ok thanks !! Bạn có bất kỳ ý tưởng về làm thế nào để bắt đầu dịch vụ để gieo thông báo? –

Trả lời

2

sử dụng báo động Android và ở trong đó cấp phát ý định

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); 
4

Trường "khi" của một thông báo được sử dụng để sắp xếp các thông báo trong thanh trạng thái. Nó không được sử dụng để kích hoạt thông báo vào thời gian quy định.

Nếu bạn muốn kích hoạt một hành động tại một thời điểm sử dụng AlarmManager quy định: Dịch vụ http://developer.android.com/reference/android/app/AlarmManager.html

Các vấn đề liên quan