1

I am using AlarmManager to periodically check for new content at some endpoint, validate if the results coming from the endpoint are the same as the ones I already have on my app, and if its not the same create a notification for each item.

What i need to know is how should i make the alarms to start only when the application is paused or stopped and cancel the alarms when de application is started or resumed.

where should i start the alarms and where should i cancel them?

In Android Notifications Guideline it says (on chapter: When not to display a notification):

Don't create a notification if the relevant new information is currently on screen. Instead, use the UI of the application itself to notify the user of new information directly in context. For instance, a chat application should not create system notifications while the user is actively chatting with another user.

If I have the application open i just want to disable alarms, when the application is closed/paused i want to cancel everything.

Bugdr0id
  • 2,962
  • 6
  • 35
  • 59

3 Answers3

3

you can try using a service and override in it , the onTrimMemory method and show the notificaton when "level" is equal to TRIM_MEMORY_UI_HIDDEN

@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    switch (level) {
        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:

            break;
    }

} 

check the documentation for more info http://developer.android.com/reference/android/content/ComponentCallbacks2.html#TRIM_MEMORY_UI_HIDDEN

ingyesid
  • 2,864
  • 2
  • 23
  • 21
  • Could you explain a little more how does onTrimMemory works for me? I did read the documentation but its not clear. – Bugdr0id Aug 19 '15 at 07:32
  • 1
    When you application UI goes into the background the TRIM_MEMORY_UI_HIDDEN ComponentCallback is received, if registered. – roomtek Oct 23 '15 at 05:17
3

You need to create a Custom Application with global state and implement your own onPause and onResume at Application Level.

Create your own subclass of Application like this:

public class MyApplication extends Application {

    private static MyApplication sInstance;

    public MyApplication getInstance(){
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
    }

    public void onStart() {
        // TODO: Stop your notification.
    }

    public void onStop() {
        // TODO: Start your notification.
    }

}

Specify its name in your AndroidManifest.xml's tag:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:name="MyApplication">

Create a class to hold the counts of activities:

public class ActiveActivitiesTracker {

    private static int sActiveActivities = 0;

    public static void activityStarted()
    {
        if (sActiveActivities == 0) {
            // TODO: Here is presumably "application level" resume
            MyApplication.getInstance().onStart();
        }
        sActiveActivities++;
    }

    public static void activityStopped()
    {
        sActiveActivities--;
        if (sActiveActivities == 0) {
            // TODO: Here is presumably "application level" pause
            MyApplication.getInstance().onStop();
        }
    }
}

Then create a base activity (or do that in every activity), simply call the activityStarted() and activityStopped() methods:

@Override
public void onStart() {
    super.onStart();
    ActiveActivitiesTracker.activityStarted();
}

@Override
public void onStop() {
    super.onStop();
    ActiveActivitiesTracker.activityStopped();
}

For more details about Custom Application, see this.

For more details about Android Application Level Pause and Resume, see this.

Hope this helps.

Community
  • 1
  • 1
  • Thank you your code example, i see where are you going with this, but in this case i will start an alarm every time ANY activity stops. I want to start the alarm not when each activity finishes, but when the application itself pauses or stops. – Bugdr0id Aug 19 '15 at 07:33
  • I must have explained something wrong... Each time you start an activity, the `ActiveActivitiesTracker` class adds one to the counter `sActiveActivities`. If for some reason your activity call the method `onStop()` and his `sActiveActivities` counter is zero, it means that all its activities are stopped. At that time, it will be called by delegation the method `onStop()` of the Application: `MyApplication.getInstance().onStop();`. This way, you will have a onStop method at the level of application and not activity. You can thus use it to create your alarm whenever your application stop. – Marcello Galhardo Aug 19 '15 at 12:29
0

I am not sure that this will be feasible within your project or if it will achieve what you hope to, however you could extend all of your activities from one base activity. In that base activity's onPause/onStop/onDestroy methods start the alarms and in that base activities onCreate/onStart methods cancel the alarms with the pending intent.

This will provide you with a set location from which you can handle your alarms if you have multiple activities from which the app may close.

You can learn more about the life cycle of activities here.

advdev1234
  • 30
  • 8