java – AlarmManager. AlarmClockinfo. Getnextalarmclock() caused NullPointerException

problem

When I try to use AlarmManager The alarmclockinfo object getnextalarmclock() is thrown by my application: trying to call the virtual method 'long Android. Net' on an empty object reference app. AlarmManager $AlarmClockInfo. getNextAlarmClock()’

My code

Relevant parts 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,10,35,55));

        // set up the service if necessary
        if ( my_svc == null ) {
            my_svc = PendingIntent.getService(c,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 information

I'm developing a home / lock screen widget application for Android The information in the widget needs to be updated at a very specific time, rather than broadcasting AppWidget on the OS_ During update operation I have successfully followed this tutorial on using AlarmManager to update widgets at specific times and can use AlarmManager The setrepeating () function updates my widget every minute

However, instead of updating the widget every minute, I update it at a specific time every 2-3 days So I tried to use AlarmManager Setexact() instead of setrepeating() The application compiles without errors and seems to work well, but the widget will not be updated when I think it should I have confirmed that the correct time is submitted to the setexact() function, but since the update did not occur as planned, I want to know whether the alarm is really set At that time, I tried to use getnextalarmclock () and began to encounter these problems

Application / emulator specification

Application:

> minSdkVersion:21 > targetSdkVersion:22

Emulator:

>Name: Nexus 5 API 22 > CPU / ABI: Intel Atom (x86) of Google API > target: Google API (API level 22) > SD card: 100M > enable GPU: Yes > ram size: 2GB

What I've tried

>Record alarm_ time. getTime(). ToString to confirm that I am setting the correct alarm time > confirm that the alarm time is in the future (by manually setting the time on the simulator) > solutions suggested by this so thread > variants of getnextalarmclock(), including the use of deprecated constants, gettriggertime() and m.getnextalarmclock() toString()

I'm completely saddened by this If you have any idea why I can't get information about the next alarm clock, or why the alarm clock I set with setexact() doesn't ring, I will be a happy boy thank you!!!

Solution

Alarmclock and AlarmManager are two completely different things: the alarm clock is set by the clock application and other applications through setalarmclock (). It is suitable for waking up users, and users of the whole system can see it (depending on the manufacturer and version of Android)

This is completely different from setexact (), which only sets programmatic alerts for your application - not visible to users You can dump the current list of alerts using ADB shell dumpsys alerts to help debug whether alerts are properly scheduled

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>