Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SetInexactRepeating isn't firing at all

I have an android setInexactRepeating placed inside of my onCreate that never fires. I have a log inside of it to make sure that it's actually executing, and that doesn't seem to fire, as well as the events that I have planned for it. I want it to go off every 10 seconds, but it doesn't even seem to go off even the first time.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d("Restart", "First");
    Intent toRun = new Intent(this, AlarmRestart.class);
    PendingIntent pendingToRun = PendingIntent.getBroadcast(this, 0, toRun, 0);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.cancel(pendingToRun); 
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 10000L, pendingToRun);

    Log.d("Restart", "Second");
}

This is in the other file:

public class AlarmRestart extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Log.d("Restart", "Third");
    }
}

This is what "adb shell dumpsys alarm" says

com.packageName.restart
5715ms running, 64 wakeups
3 alarms: flg=0x14 cmp=com.packageName.restart/.AlarmRestart
61 alarms: flg=0x14 cmp=com.packageName.restart/.reciever.AlarmRestart
like image 712
Lethjakman Avatar asked Dec 06 '22 13:12

Lethjakman


2 Answers

AlarmRestart is a BroadcastReceiver. It is not a Service. Yet you are trying to use a getService() PendingIntent. That will not work. Change getService() to getBroadcast(), and you should have better luck.

like image 153
CommonsWare Avatar answered Dec 26 '22 02:12

CommonsWare


I found the problem :/

        <reciever android:name="com.appName.restart.AlarmRestart" android:enabled="true" />

It's spelled receiver....not reciever. Seriously, XML needs an error checker. I'm still marking CommonWares answer as the accepted one, because he solved several other issues that I would have run into later.

like image 39
Lethjakman Avatar answered Dec 26 '22 04:12

Lethjakman