Search code examples
androidalarmmanager

Alarm not called on Android


So I am trying to implement the repeating feature from this site - http://www.java2s.com/Code/Android/Core-Class/Exampleofschedulingoneshotandrepeatingalarms.htm - into my Android app but it's not calling the class for some reason here is the code excerpts.

public class MainActivity extends Activity implements SensorEventListener {

// Schedule repeating alarm

          Intent intent = new Intent(MainActivity.this, RepeatingAlarm.class);
          PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0,
              intent, 0);

          // We want the alarm to go off 30 seconds from now.
          long firstTime = SystemClock.elapsedRealtime();
          firstTime += 15 * 1000;

          // Schedule the alarm!
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
              15 * 1000, sender);

          // Tell the user about what we did.
            Toast.makeText(
                    getApplicationContext(),
                    "Scheduled", Toast.LENGTH_LONG)
                    .show();

}

//Schedule alarm for data upload
class RepeatingAlarm extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
            Toast.makeText(
                    context,
                    "ALARMED!", Toast.LENGTH_LONG)
                    .show();
      }
}

If it helps the full code is here: https://gist.github.com/4410665


Solution

  • The solution was to take:

    //Schedule alarm for data upload
    class RepeatingAlarm extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
                Toast.makeText(
                        context,
                        "ALARMED!", Toast.LENGTH_LONG)
                        .show();
          }
    }
    

    And save it as RepeatingAlarm.java basically it's own class. Then go into the mainfest file and add something of this effect

    <receiver android:name="RepeatingAlarm"></receiver>