1

The plan for the app is to fire a notification everyday at 7 a.m., which actually works already. The things is that the first time the app is started, it asks for login data which also works in general, but the app crashes if the user hasn't submitted fast enough.

This is the code in the MainActivity which runs if the user hasn't already submitted:

Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, 7);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);

                    Intent intent1 = new Intent(this, NotificationClass.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent1, 0);
                    AlarmManager am = (AlarmManager) getSystemService(MainActivity.this.ALARM_SERVICE);
                    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

I guess NotificationClass is being started because the "source" of the crash is code of the loadText.java which is started by NotificationClass. The plan is hat it actually only starts everyday at 7 a.m. as mentioned. Does someone know this problem or has an idea? I already tried different flags.

Thanks in advance!

Error in logcat:

08-22 14:54:02.217 2762-2762/de.kurt.vertretungsplan E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: de.kurt.vertretungsplan, PID: 2762
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'org.jsoup.select.Elements org.jsoup.nodes.Element.select(java.lang.String)' on a null object reference
                                                                           at de.kurt.vertretungsplan.loadText$loadTextAsyncTask.onPostExecute(loadText.java:112)
                                                                           at de.kurt.vertretungsplan.loadText$loadTextAsyncTask.onPostExecute(loadText.java:35)
                                                                           at android.os.AsyncTask.finish(AsyncTask.java:667)
                                                                           at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                           at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
08-22 14:54:02.232 1247-1293/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-22 14:54:02.295 1462-3018/system_process E/EGL_emulation: tid 3018: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
quidproquo
  • 27
  • 9
  • It's really not clear what the problem is, but if you're asking for a fix for the alarm firing immediately, have a look here: https://stackoverflow.com/questions/36535575/android-prevent-immediate-trigger-of-alarm-service-if-alarm-time-has-passed-for. – Mike M. Aug 22 '17 at 21:47
  • Thank you :). It works now! – quidproquo Aug 25 '17 at 16:00

1 Answers1

1

You get a Calendar instance and set the time to 7:00:00. This sets the Calendar time to 7:00:00 on the current day.

Then you do this:

am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

This sets an alarm that will go off at 7:00:00 today and then at 7:00:00 every day after that. Assuming that you run this code after 7:00:00, the alarm will trigger immediately because the alarm time is in the past.

To prevent this, you need to make sure that the Calendar time is greater than the current time. If it isn't, you should add one day to the Calendar time before calling am.setRepeating().

As @MikeM. suggested, see android prevent immediate trigger of alarm service if alarm time has passed for the day for more details.

David Wasser
  • 93,459
  • 16
  • 209
  • 274