Как запустить уведомление в произвольное время? - PullRequest
5 голосов
/ 30 января 2012

Я хочу установить свое уведомление в 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);

Ответы [ 2 ]

4 голосов
/ 30 января 2012

Поле «когда» уведомления используется для сортировки уведомления в строке состояния. Он не используется для запуска уведомления в указанное время.

Если вы хотите запустить действие в указанное время, используйте AlarmManager: http://developer.android.com/reference/android/app/AlarmManager.html

2 голосов
/ 01 марта 2013

использовать службы тревоги 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...