All good! Guys, it's me again and I need your help. I completely rethought work with time, with date pickers and other useful things. There is the following code.


1. Method of working with time:

... info = (TextView)mBottomSheetDialogTime.findViewById(R.id.info); pickerDate = (DatePicker)mBottomSheetDialogTime.findViewById(R.id.pickerdate); pickerTime = (TimePicker)mBottomSheetDialogTime.findViewById(R.id.pickertime); Calendar now = Calendar.getInstance(); pickerDate.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), null); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { pickerTime.setHour(now.get(Calendar.HOUR_OF_DAY)); pickerTime.setMinute(now.get(Calendar.MINUTE)); } else { pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY)); pickerTime.setCurrentMinute(now.get(Calendar.MINUTE)); } buttonSetAlarm = (Button)mBottomSheetDialogTime.findViewById(R.id.setalarm); buttonSetAlarm.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { Calendar current = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) cal.set(pickerDate.getYear(), pickerDate.getMonth(), pickerDate.getDayOfMonth(), pickerTime.getHour(), pickerTime.getMinute(), 0); else cal.set(pickerDate.getYear(), pickerDate.getMonth(), pickerDate.getDayOfMonth(), pickerTime.getCurrentHour(), pickerTime.getCurrentMinute(), 0); Context context = getApplicationContext(); if(cal.compareTo(current) <= 0) Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show(); else scheduleNotification(getNotification(), cal); }}); ... 

2. Notification work method:

 private void scheduleNotification(Notification notification, Calendar targetCal{ info.setText("\n\n\n"+"Alarm is set@ "+targetCal.getTime()+"\n"); Intent notificationIntent = new Intent(getBaseContext(), NotificationPublisherBroadcastReceiver.class); notificationIntent.putExtra(NotificationPublisherBroadcastReceiver.NOTIFICATION_ID, 1); notificationIntent.putExtra(NotificationPublisherBroadcastReceiver.NOTIFICATION, notification); PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, notificationIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); if (android.os.Build.VERSION.SDK_INT >= 19) alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); else alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); } 

3. Receiver

  public class NotificationPublisher extends BroadcastReceiver{ public static String NOTIFICATION_ID = "notification-id"; public static String NOTIFICATION = "notification"; public void onReceive(Context context, Intent intent){ NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = intent.getParcelableExtra(NOTIFICATION); int id = intent.getIntExtra(NOTIFICATION_ID, 0); notificationManager.notify(id, notification); Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show(); } } 

4. Manifest

 <receiver android:name=".NotificationPublisher" android:process=":remote"/> 

5. getNotification ()

 private Notification getNotification() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Intent intent = new Intent(getBaseContext(), NotificationPublisherBroadcastReceiver.class); intent.putExtra("title", getString(R.string.notificationtitle)); intent.putExtra("text", getString(R.string.notificationtext)); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext()) .setSmallIcon(R.drawable.time) .setLargeIcon(bitmap) .setContentTitle(getString(R.string.notificationtitle)) .setContentText(getString(R.string.notificationtext)) .setContentIntent(pIntent) .setAutoCancel(true) .setVibrate(new long[]{1000, 1000, 1000, 1000}) .setDefaults(Notification.DEFAULT_SOUND); return builder.build(); } 

The whole problem is that when I click buttonSetAlarm , in which the notification call is scheduleNotification(getNotification(), cal); notifications come right away, not when I set the time, and Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show(); is not shown yet. show Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show(); from the receiver (There is a suspicion of non-API support. I have support from version 15 to version 23, at the moment I am testing at version 23). Where am I wrong? And what should I do to fix it? Any help would be priceless!

    1 answer 1

    This is how the alarm is registered for different versions of android-a.

      if (android.os.Build.VERSION.SDK_INT >= 19) alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); else alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

    Plus, see what value pickerDate.getMonth() takes. If it returns 5 (May), then it is necessary to subtract 1, since the month calendar contains from 0 to 11.

    • I did it, but I still don’t plow :( Why? Toast.makeText (context, "Alarm received!", Toast.LENGTH_SHORT) .show ();
    • and you watched under debugging? Does he go there exactly? Is the correct time set in the calendar? - Android Android
    • Everything earned! Your condition helped :) Thank you very much), the mistake was that I kept the recycler in another package ... As always - iFr0z
    • =) It happens. notchto - Android Android
    • The truth is that the funny situation turns out, he immediately displays the notification and then displays the notification at the right time, do not know where I am again?) - iFr0z