-1

I have implemented an Alarm manager and receiver in my application, all is working perfectly. The issue I am having is when I press the back button to close the application, the alarm doesn't run at the time specified. Below is the code I am using:

My Receiver Code

    public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("SERVICE RECIEVED");
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
    }

}

My Alarm Service Code

public class MyAlarmService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        Intent _intent = new Intent(getBaseContext(), FirstCallActivity.class);
        _intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(_intent);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}

Start Alarm Code

public void startAlarm(Calendar cal) {

    // Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(this, FirstCallActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}

Manifest Code (within application tags)

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

    <receiver android:name=".MyReceiver" />

Could anyone explain why this is happening? I know if the application is fully closed nothing is going to happen, but I it seems strange the back button causing this issue because the application is still running in the background.

Thanks

Phil Andrews
  • 409
  • 3
  • 23
  • Where do you call `startAlarm()`? I don't see it anywhere. Also, does it run just not when expected if you press the back button or it doesn't run at all? – codeMagic Dec 19 '14 at 15:07
  • The alarm is called from the main activity in a BaseActivity class. If the app is left open or just minimised it works fine, but if I press the back button the app doesn't fire until I launch the app again and it fires – Phil Andrews Dec 19 '14 at 15:08
  • Where does the Receiver fit into this? You're not broadcasting to it anywhere. Also, what's the end goal, here? You want your alarm to start the Activity? I'm not following what intended flow of execution is. – Mike M. Dec 19 '14 at 15:20
  • The end goal is to launch the Activity, which currently works as I intended. Its just having an issue when the back button is pressed to close the app, even though the app is still running in the background. – Phil Andrews Dec 19 '14 at 15:38
  • I have just found out that the application is closing on the back button press, I have implemented a fix that is stopping the app from completely closing on the back button press which is allowing the functions to run correctly in the background. – Phil Andrews Dec 19 '14 at 15:44
  • Hi, What is the solution for this issue? I am also facing the same –  Mar 22 '18 at 10:21

1 Answers1

0

It turns out that the app was being closed on the back button. By stopping the app closing on the back button press with the code below, the function required for the set alarm run correctly.

@Override
    public void onBackPressed() {
        System.out.println("BACK PRESS");
        moveTaskToBack(true);
    }
Phil Andrews
  • 409
  • 3
  • 23