Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager.AlarmClockinfo.getNextAlarmClock() causes NullPointerException

Tags:

java

android

The Problem

When I try to use the AlarmManager.AlarmClockInfo object to getNextAlarmClock() my app throws: Attempt to invoke virtual method 'long android.app.AlarmManager$AlarmClockInfo.getNextAlarmClock()' on a null object reference

My Code

The relevant section of my code...

public class MyAppWidget extends AppWidgetProvider {

    private PendingIntent my_svc = null;

    public void onUpdate(Context c, AppWidgetManager awm, int[] appWidgetIds) {

        final AlarmManager m = (AlarmManager)c.getSystemService(Context.ALARM_SERVICE);
        final Calendar alarm_time = Calendar.getInstance();
        final Intent i = new Intent(c, MyService.class);

        // set alarm for 2015-Jul-7 10:35:55 AM
        alarm_time.setStime(new Date(2015,7,7,10,35,55));

        // set up the service if necessary
        if ( my_svc == null ) {
            my_svc = PendingIntent.getService(c, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        }

        // set the alarm
        m.setExact(AlarmManager.RTC, alarm_time.getTime().getTime(), my_service);

        // now write a log message to confirm the alarm was set
        // **THIS IS THE CODE THAT THROWS THE EXCEPTION**
        AlarmManager.AlarmClockInfo aci = m.getNextAlarmClock();
        Log.v("MyAppWidget", "next alarm at: " + aci.toString());
    }
}

Background Info

I'm developing a home/lock screen widget app for Android. The info in the widget needs to be updated at very specific times other than when the OS broadcasts the APPWIDGET_UPDATE action. I successfully followed this tutorial on using AlarmManager to update widgets at specific times, and was able to use the AlarmManager.setRepeating() function to update my widget once a minute.

However, I don't need the widget updated every minute, but only once at a specific time that happens every 2-3 days. So I tried using AlarmManager.setExact() instead of setRepeating(). The app compiles without error and appears to run just fine, but the widget doesn't update when I think it is supposed to. I've confirmed that the correct time is being submitted to the setExact() function, but since the update doesn't happen as scheduled I wanted to see whether or not the alarm was really set. That's when I tried using getNextAlarmClock() and began having these problems.

App/Emulator Specs

App:

  • minSdkVersion: 21
  • targetSdkVersion: 22

Emulator:

  • Name: Nexus 5 API 22
  • CPU/ABI: Google API's Intel Atom (x86)
  • Target: Google APIs (API level 22)
  • SD Card: 100M
  • GPU Enabled: yes
  • RAM Size: 2GB

Things I've tried

  • Logging alarm_time.getTime().toString to confirm I'm setting the correct alarm time
  • Confirmed that the time of the alarm is in the future (by manually setting the time on the emulator)
  • Solutions suggested by this SO thread
  • Variations on getNextAlarmClock() including using the deprecated constant, getTriggerTime() and m.getNextAlarmClock().toString()

I'm totally stumped by this. If you have any thoughts about why I can't get info about the next set alarm, or why my alarm set with setExact() doesn't go off, I'll be a happy boy. Thank you!!!

like image 363
morphatic Avatar asked Oct 27 '25 11:10

morphatic


1 Answers

AlarmClock and AlarmManager are two entirely different things: alarm clocks are set by the Clock app and other apps via setAlarmClock() and are suitable for waking people up and is visible to the user throughout the system (where depending on the manufacturer and version of Android).

That is entirely different from setExact(), which only sets a programmatic alarm for your app - nothing visible to the user. You can use adb shell dumpsys alarm to dump the list of current alarms to help debug whether your alarm was scheduled correctly.

like image 164
ianhanniballake Avatar answered Oct 29 '25 09:10

ianhanniballake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!