morphatic
morphatic

Reputation: 7945

AlarmManager.AlarmClockinfo.getNextAlarmClock() causes NullPointerException

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:

Emulator:

Things I've tried

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!!!

Upvotes: 4

Views: 5647

Answers (4)

Simon
Simon

Reputation: 2066

getNextAlarmClock() can return null. That cause the exception when you do

Log.v("MyAppWidget", "next alarm at: " + aci.toString())

Instead do this:

AlarmManager.AlarmClockInfo aci = m.getNextAlarmClock();
if (aci==null)
   Log.v("MyAppWidget", "no alarm");
else
   Log.v("MyAppWidget", "next alarm at: " + aci.toString());

Upvotes: 1

bonstana
bonstana

Reputation: 44

getNextAlarmClock() Is meant for alarms set by setAlarmClock() only. You used setExact() and that's why you are having the bugs.

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 199805

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.

Upvotes: 10

Ben
Ben

Reputation: 1295

If you look at the javadoc for getNextAlarmClock http://developer.android.com/reference/android/app/AlarmManager.html#getNextAlarmClock()

it says "The alarm clocks considered are those scheduled by setAlarmClock(AlarmManager.AlarmClockInfo, PendingIntent) from any package of the calling user." I'm not sure why that's the case but since you're using setExact my guess is that's why it's null when you call getNextAlarmClock.

Upvotes: 3

Related Questions