2013-02-11 3 views
1

Я новичок в android. Я хочу установить уведомление локально. И я хочу запустить его в определенный день и время. Но если я поставлю его в будущее, он выстрелится немедленно?
Уведомление о пожарах Немедленно

Calendar calendar = Calendar.getInstance();  

     //---sets the time for the alarm to trigger--- 
     calendar.set(Calendar.YEAR, 2013); 
     calendar.set(Calendar.MONTH, 1); 
     calendar.set(Calendar.DAY_OF_MONTH, 11);     
     calendar.set(Calendar.HOUR_OF_DAY, 15); 
     calendar.set(Calendar.MINUTE, 54); 
     //calendar.set(calendar.AM_PM,1); 
     calendar.set(Calendar.SECOND, 0); 


     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.alp_btn_code_lock_touched_holo;   
     CharSequence tickerText = "Hello"; // ticker-text 
     long when = System.currentTimeMillis();   
     Context context = getApplicationContext();  
     CharSequence contentTitle = "Hello"; 
     CharSequence contentText = "Hello";  
     Intent notificationIntent = new Intent(this, Login.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     Notification notification = new Notification(icon, tickerText, calendar.getTimeInMillis()); 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 


     mNotificationManager.notify(1, notification); 

Спасибо за помощь

ответ

0

Используйте Alarmmanager в Broadcasrreceiver это полезно по мере изменения кода, как этот

Calendar calendar = Calendar.getInstance();  

    //---sets the time for the alarm to trigger--- 
    calendar.set(Calendar.YEAR, 2013); 
    calendar.set(Calendar.MONTH, 1); 
    calendar.set(Calendar.DAY_OF_MONTH, 11);     
    calendar.set(Calendar.HOUR_OF_DAY, 15); 
    calendar.set(Calendar.MINUTE, 54); 
    //calendar.set(calendar.AM_PM,1); 
    calendar.set(Calendar.SECOND, 0); 

    Intent intentval = new Intent(YourActivity.this, TimeAlarm.class); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentval, 0); 
    alarm.set(AlarmManager.ELAPSED_REALTIME, calendar.getTimeInMillis() , pendingIntent); 

в TimeAlarm.class использования, как это

public class TimeAlarm extends BroadcastReceiver { 
NotificationManager nm; 

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

    int notificationId = Integer.parseInt(intent.getData().getSchemeSpecificPart());  

    Intent alarmIntent = new Intent(context, TimeAlarm.class); 
    alarmIntent.setData(Uri.parse("timer:" + notificationId)); 
    //alarmIntent.setAction(String.valueOf(alarm.ID)); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent displayIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
    alarmManager.cancel(displayIntent);  

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 
    Notification notif = new Notification(icon, tickerText, System.currentTimeMillis()); 
    notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    nm.notify(1, notif);   
+0

Но используя эту проблему, моя проблема будет решена? –

+0

может быть первым, мы проверяем ... – NagarjunaReddy