0

I'm attempting to make it so my app runs some code once per day at 6AM. This works just fine when the app is open and in the foreground, but if the app is closed by swiping it away, the code is never called at the appropriate time.

AlarmReceiver.java (For testing purposes, I have it just trying to display a Toast to verify it runs)

public class AlarmReceiver extends BroadcastReceiver {

    public static final String intentAction = "com.mpagliaro98.action.NOTIFICATIONS";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(intentAction)) {
            Toast.makeText(context, "RECEIVER CALLED", Toast.LENGTH_LONG).show();
        }
    }
}

MainActivity.java (Where the alarm is being set)

public class MainActivity extends AppCompatActivity {

    ...

    private void setRecurringAlarm() {
        // Set this to run at 6am
        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault());
        updateTime.set(Calendar.HOUR_OF_DAY, 6);
        updateTime.set(Calendar.MINUTE, 0);
        updateTime.set(Calendar.SECOND, 0);
        updateTime.set(Calendar.MILLISECOND, 0);

        // Build the pending intent and set the alarm
        Intent i = new Intent(AlarmReceiver.intentAction);
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
                0, i, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        assert am != null;
        am.setRepeating(AlarmManager.RTC, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
    }
}

AndroidManifest.xml (Just the relevant parts)

<uses-permission android:name="android.permission.SET_ALARM" />

<receiver
    android:name=".AlarmReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.mpagliaro98.action.NOTIFICATIONS" />
    </intent-filter>
</receiver>

I've read through dozens of similar problems to this on this site and elsewhere and I'm seriously at a loss for why this won't work. Any help would be appreciated.

1 Answers1

0

Try changing receiver to

<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>

Should I use android: process =":remote" in my receiver?

RAMU
  • 176
  • 1
  • 8
  • I've added that and unfortunately it hasn't made a difference. In fact, today it's being really inconsistent with calling the receiver when the app is open; I'm trying to test it by setting the interval between alarms to one minute, but now even when the app is open it only fires maybe a quarter of the time – mpagliaro98 Mar 15 '20 at 14:46