Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm Manager not working when app closed

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?

like image 203
mocode10 Avatar asked Nov 22 '17 01:11

mocode10


People also ask

Does alarm Manager persist even after reboot?

The AlarmManager service is a convenient class to schedule working on your Android application; however, when the device shuts down or reboots, all your alarms will be lost since the system does not retain them between system restarts.

How do I use alarm Manager on Android?

This example demonstrates how do I use AlarmManager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I turn off alarm manager on Android?

In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver. class); PendingIntent pendingIntent = PendingIntent.

How to set alarm for particular time in Android?

In the main activity, AndroidTimeActivity, the main code to start Alarm is in setAlarm(Calendar targetCal) method. Retrieve AlarmManager by through getSystemService(Context. ALARM_SERVICE). And set our alarm with alarm type(RTC_WAKEUP), trigger time, and pendingIntent.


2 Answers

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);
}

}
like image 86
Saurabh Mistry Avatar answered Oct 04 '22 20:10

Saurabh Mistry


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

like image 30
hidd Avatar answered Sep 30 '22 20:09

hidd