1

Есть класс, который наследуется от Service, этот класс вызывает уведомление.

    public void sendNotification() {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

    //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.androidauthority.com/"));
 //   Context context = getApplicationContext();

    Intent intent = new Intent(this,ActivityFragments.class);
    intent.putExtra("ExamFragment","ExamFragment");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

            mBuilder.setContentIntent(pendingIntent);
            mBuilder.setSmallIcon(R.drawable.icon);
            mBuilder.setContentTitle("Уведомление");
            mBuilder.setContentText("Вам нужно срочно проверить результаты");

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 10);
    calendar.set(Calendar.MILLISECOND, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

    mNotificationManager.notify(001, mBuilder.build());

}

А вот кнопка в фрагменте:

    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // NotificationService service = new NotificationService();
            getActivity().startService(new Intent(getActivity(),NotificationService.class));

        }
    });

При нажатии на кнопку,уведомление вызывается сразу, как сделать так, чтобы оно вызывалось через определенное время?

0

1 ответ 1

3

AlarmManager ничего про нотификации не знает. Вам нужно будет сделать BroadcastReceiver, который будет вызван AlarmManager'ом и покажет нотификацию.

Посмотрите мой ответ здесь: https://ru.stackoverflow.com/a/611584/193715

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.