Search code examples
androidalarmmanager

Alarm not triggering in android


I am new to android programming. What am i doing wrong? AlarmReceiver class not triggering...I want to trigger repeatedly the AlarmReceiver.class ...

I used the code from this page https://developer.android.com/training/scheduling/alarms#precision

OnCreate at MainActivity.java i have the following code

   AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
      
        Intent intent = new Intent(this,AlarmReceiver.class);
        //Check if the PendingIntent already exists
PendingIntent pendingIntent =
                PendingIntent.getService(this, 6661, intent,
                        PendingIntent.FLAG_NO_CREATE);
        if (pendingIntent != null && alarmManager != null) {
           //cancel here if you want
        }

        //Run every 60 seconds!
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 60000,
                60000, pendingIntent);

The AlarmReceiver.java class includes the following code...

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // For our recurring task, we'll just display a message
        Log.i("logit","testing...");
       
    }
}

Solution

  • Do you have an entry in your manifest for the receiver like that?

    <receiver android:name=".AlarmReceiver"></receiver>