1

I have an AlarmManager that shows a Toast every 10 mins.but when os kill apps in the background , so my AlarmManager not work any more. what I have to do?

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context con, Intent arg1) {
        Global.ShowMessage(con, Global.GetCurrentDateTime());

    }
}

In Manifest.xml :

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

in main Activity :

 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);

        long time = cal.getTimeInMillis();

        Intent i = new Intent(this, MyReceiver.class);

        PendingIntent pi = PendingIntent.getBroadcast(this, 9854, i,  PendingIntent.FLAG_UPDATE_CURRENT);

        // am.set(AlarmManager.RTC_WAKEUP,time,pi);

        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 600 * 1000, pi);
Gopal
  • 1,734
  • 1
  • 14
  • 34
m n
  • 223
  • 1
  • 7
  • 19
  • Since it is linked to the main thread, this is normal. You would need to use a Service that have more chance to survive some system clearance but without certitude – AxelH Dec 29 '16 at 09:23
  • In your manifest declaration of the receiver you need `android:enabled="true"` and `android:exported="true"` – Nick Friskel Dec 29 '16 at 09:24
  • @nickfriskel Have you some source for this ? I always thought that `exported` was used to let the entity be visible by other app and enabled is quite obvious. IT doesn't have any to do with some system kill signal to recover some memory – AxelH Dec 29 '16 at 09:27
  • Sorry I didn't realize it was the actual alarm which is being cancelled. So there is no record in the `adb shell dumpsys alarm` after the app is closed? Or only when it is 'Force Stopped'? – Nick Friskel Dec 29 '16 at 09:32
  • can you take a look at my question that was based on this only...http://stackoverflow.com/questions/41277671/clear-recent-apps-wipe-the-apps-memory-and-my-receiver-stopped-working – Mr.Popular Dec 29 '16 at 10:08

2 Answers2

1

From the official documentation on the lifecycle

The system never kills an activity directly. Instead, it kills the process in which the activity runs, destroying not only the activity but everything else running in the process, as well.

But you can create a Service that will not be killed with the Activity. To be more specific, you want a Service that run in foreground, this will not be killed by the system as stated in the Services documentation

For a specific example, I prefer to let Google guide you with this [example](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)).

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • I am calling alaram manager in my mainActivity.is it becouse of that? – m n Dec 29 '16 at 09:55
  • startForeground show a notification that idont want that – m n Dec 29 '16 at 09:56
  • @mn For the first comment, yes, this is link to the Activity so it will be destoyed. For the notification, it is there to keep the service alive (and notify the user that this is alive). You could create a background service but it can be killed like an activitiy. – AxelH Dec 29 '16 at 10:37
-3

Append this code in manifest.

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

                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>

            </receiver>
Ankur Khandelwal
  • 1,042
  • 3
  • 19
  • 40
  • This will only launch the app on boot (and not for every device). Not after a kill signal from the system to gain some memory – AxelH Dec 29 '16 at 09:24
  • No I did not thinks so. I am using the the alarm manager to as a reminder. and It is working fine. – Ankur Khandelwal Dec 29 '16 at 09:27
  • 2
    Do you understand the code you have post ? The action `BOOT_COMPLETED` is here to create the receiver on this action, this action is sended when the device is started (some devices use different action). But this will not prevent the system to kill the app and the receiver link to it. Unless you can provide some source of course that show that I am wrong (which is always possible ;) ) – AxelH Dec 29 '16 at 09:30