0

I'm running my android app.

I want to enable push notifications only when app is not visible

(closed or in the background).

I have seen this code:

  ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++){
        if(procInfos.get(i).processName.equals("com.android.browser")) {
            Toast.makeText(getApplicationContext(), "Browser is running", Toast.LENGTH_LONG).show();
        }
    }

for checking if the browser is on,

but it's not enough for me to check if the process is on,

it can be running but not visible at the moment,

and i want the notifications show in that case as oppose to when it's visible.

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 1
    [CommonsWare posted about an interesting solution](http://commonsware.com/blog/2010/08/11/activity-notification-ordered-broadcast.html) using ordered broadcasts. It might be worth looking into that. – Mattias Buelens Apr 14 '14 at 13:49

2 Answers2

1

The Approach that you could follow in this case is :

Make a service that post notifications. and onStart() or onResume() ,stop the service and onPause() or onStop() ,start service again.

Dilroop Singh
  • 544
  • 6
  • 16
1

Check out this answer: https://stackoverflow.com/a/12675356/3432809 - but note the caveat

Other option is to keep track of your Activities in a shared location (say, the Application object), so that each onResume() call sets a flag that the Activity is running, and onPause() clears the flag.

Community
  • 1
  • 1
Mikko Liikanen
  • 675
  • 4
  • 8