Bugdr0id
Bugdr0id

Reputation: 3063

Android - Display notification when application is in background

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.

Upvotes: 1

Views: 1082

Answers (3)

Marcello Galhardo
Marcello Galhardo

Reputation: 791

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.

Upvotes: 3

advdev1234
advdev1234

Reputation: 30

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.

Upvotes: 0

ingyesid
ingyesid

Reputation: 2884

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

Upvotes: 3

Related Questions