Search code examples
androidbroadcastreceiveralarm

alarm not triggering when app is no running


I'm setting up a daily alarm. It works if the app is running at alarm time, but does not work if the app is not running. This is how I declare the receiver in the Manifest:

<receiver android:name="com.myAppPackage.alarm.AlarmReceiver"
        android:enabled="true"
        android:exported="true"/>

Studio warns me: Exported receiver does not require permission.

True I have not added an android:permission nor and Intent to the receiver and the application section doesn't have any permission tags.

And this is the broadcastreceiver:

    package com.myAppPackage.alarm;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

    public class AlarmReceiver extends BroadcastReceiver {

    public AlarmReceiver(){}

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

        final Intent syncIntent = new Intent(context, AlarmActivity.class);

        syncIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(syncIntent);

    }
}

The alarm is configured in the following method (in this example configured to set-off daily inexact at 13:48):

public static void configureDailySync(Context context) {

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent alarmIntent = new Intent(context, AlarmReceiver.class);

    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    final int hourOfDay = 13;
    final int minuteOfHour = 48;

    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minuteOfHour);

    alarmManager.setInexactRepeating(
            AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY,
            alarmPendingIntent);
}

Any help is very welcome! Thank you!!

news: getting closer... if I copy the alarm-setting code in the MainActivity onCreate() method it works! This is the code I copied:

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent alarmIntent = new Intent(this, AlarmReceiver.class);

    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    final int hourOfDay = 13;
    final int minuteOfHour = 48;

    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minuteOfHour);

    alarmManager.setInexactRepeating(
            AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY,
            alarmPendingIntent);

When the alarm is created as above in the onCreate() of the MainActivity then it DOES trigger even when the app is closed... But when I call the call the method from the MainActivity like this:

MyAppAccount.configureDailySync(this);

it doesn't work! MyAppAccount is an plain class not extending anything... I've tried to have MyAppAccount extend AppCompatActivity in case it mattered but nothing... Oh well... it seems that the above try of executing the alarm-setting in the onCreate() method of the MainActivity is not always working... what is most puzzling!!! :-(


Solution

  • SOLVED: It had nothing to do with coding!! The problem was the way I was closing the application. When closing the application using the stop button of Android Studio the alarm is NOT set. When closing the application from the phone, using the back button for instance and / or removing the application from the list of applications (with the square button), then the alarm works!

    Why? No idea...