Alarm Manager not working when app closed

10,362

Solution 1

Add this in your AndroidManifest.xml

<service
android:name=".MyService"
android:enabled="true"
android:exported="true" />

<receiver
android:name=".MyAlarmReceiver"
android:enabled="true"
android:exported="true" />

MyAlarmReceiver.java

public class MyAlarmReceiver extends BroadcastReceiver {
Context context;

public MyAlarmReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    intent = new Intent(context, MyService.class);
    context.startService(intent);
}

}

MyService.java

public class MyService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

   YourTask();

  return Service.START_STICKY;
}

private void YourTask(){
    // call api in background 

   // send push notification 

   //etc...
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}


}

MainActivity.java

public class MainActivity extends AppCompatActivity {
PendingIntent pendingIntent;
AlarmManager alarmManager;
Intent alarmIntent;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    AutoUpdateDataInBackground();
    }

 private void AutoUpdateDataInBackground() {
    // Retrieve a PendingIntent that will perform a broadcast

    alarmIntent = new Intent(MainActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    long interval = 15 * 60 * 1000;

    // Repeating on every 15 minutes interval

    Calendar calendar = Calendar.getInstance();
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),interval, pendingIntent);
}

}

Solution 2

BTW : AlarmManager will not be called with locked screen and enabled energy saving mode

Share:
10,362
mocode10
Author by

mocode10

Updated on June 04, 2022

Comments

  • mocode10
    mocode10 almost 2 years

    I've been stuck on this for days now. I want my alarm manager to fire off every 15 minutes even when the app is closed but it does not work when app is closed. It works while app is open though.

    In my manifest file I have:

      <!-- Used to consume the alarm manager alerts when app clsoed -->
        <receiver
           android:name="biz.customName.pkg.AlarmReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="biz.customName.pkg.msg"/>
            </intent-filter>
        </receiver>
    

    My BroadcastReceiver class (AlarmReceiver)

    public class AlarmReceiver extends BroadcastReceiver
    {
    
    // Alarm manager used to run install when app is closed
    AlarmManager alarmManager;
    
    
    // Called when alarm received
    @Override
    public void onReceive(Context context, Intent intent)
    {
    
        // Enable alarm
        setupAlarm(context);
    
        // Perform background task 
    }
    
    
    // Setup alarm
    public void setupAlarm(Context context)
    {
        // Setup reciever for alarm
      //  context.registerReceiver(this, new IntentFilter("biz.customName.pkg.msg"));
    
        // Setup pending intent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(Loader.filterName), PendingIntent.FLAG_UPDATE_CURRENT);
    
        // Setup alarm
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    
        final long triggerTime = System.currentTimeMillis() + 900 * 1000;
    
        // Newest OS
        if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 23)
        {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
        }
     }
    }
    

    In main I call setup alarm to get the alarm going initially then each time the onReceive inside my Broadcast receiver is called I reset the alarm.

    What am I doing wrong that it doesn't work when the app is closed?