2011-11-25 136 views
1

喜IA M個新到Android .. 我已經開發了一個程序來設置通知onspecific時間 但問題是,它顯示了2011年10月之前 只通知日期,如果我進入11任何日期的不顯示任何通知.. 似乎很奇怪,但這裏真的卡住....定通知警報

主要活動..

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Calendar cal = Calendar.getInstance(); // for using this you need to 

    cal.set(Calendar.MONTH, 11); 
    cal.set(Calendar.YEAR, 2011); 
    cal.set(Calendar.DAY_OF_MONTH, 25); 
    cal.set(Calendar.HOUR_OF_DAY, 18); 
    cal.set(Calendar.MINUTE, 28); 

    Intent alarmintent = new Intent(getApplicationContext(), 
      AlarmReceiver.class); 
    alarmintent.putExtra("title", "Title of our Notification"); 
    alarmintent.putExtra("note", "Description of our Notification"); 


    int HELLO_ID = 1; 
    PendingIntent sender = PendingIntent.getBroadcast(
      getApplicationContext(), HELLO_ID, alarmintent, 
      PendingIntent.FLAG_UPDATE_CURRENT | 
      Intent.FILL_IN_DATA); 

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
} 

和報警reciever活動......

public class AlarmReceiver extends BroadcastReceiver { 

    private static final String NotificationManager = null; 
    private static int NOTIFICATION_ID = 0; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     NotificationManager manger = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, 
       "Combi Note", System.currentTimeMillis()); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 
       NOTIFICATION_ID, new Intent(context, 
        AlarmReceiver.class), 0); 
     Bundle extras = intent.getExtras(); 
     String title = extras.getString("title"); 

     String note = extras.getString("note"); 
     notification.setLatestEventInfo(context, note, title, contentIntent); 
     notification.flags = Notification.FLAG_INSISTENT; 
     notification.defaults |= Notification.DEFAULT_SOUND; 

    manger.notify(NOTIFICATION_ID, notification); 

    } 
}; 

回答

0

您正在爲5/5/2011設置警報,此日期已過,因此立即執行。

當您收到Calendar的實例時,我認爲系統會自動填寫當前日期&時間。因此,改變您需要更改(也就是說,如果你想在5天內發出通知,更改一天只日曆中的實例)

順便說一下,點擊通知其會在再次, ,請撥打電話BroadcastReceiver,這次意圖不會有有你在活動中放入的額外物品。點擊通知時,您應該啓動一項活動 - 而不是廣播接收器。

+0

我試過它從01/11/2011到25/11/2011但它不工作.... –

+0

cal.set(Calendar.MONTH,11); cal.set(Calendar.YEAR,2011); cal.set(Calendar.DAY_OF_MONTH,25); cal.set(Calendar.HOUR_OF_DAY,18); cal.set(Calendar.MINUTE,28);這是不工作.... –

+0

thnks @JONG的幫助,但你可以嘗試它在我們的日食今天的日期和給我的代碼... –

1
Calendar.set(Calendar.MONTH, month) 

期望基於0的值。 11月是十二月。

+0

謝謝..我應該給出值爲一天,一年,一小時,分鐘stc也基於0基於開始的基礎...... plzz幫助 –

+0

只有幾個月是基於0。處理月份時,最好使用常量值,如cal.set(Calendar.MONTH,Calendar.OCTOBER);有關更多詳細信息,請訪問http://developer.android.com/reference/java/util/Calendar.html。 – Hicham

+0

你應該給它們的數值,並使用常量爲litteral值(月,日的一週) – njzk2