2

I wants to determine whether my app is in foreground or background on device. In my app, When i get push notification on device then it is require to me that identify 1. whether app is in background or foreground? 2. What is/was the current/last opened activity of app.

and one more thing, I also wants to find out the check box value(checked/unchecked) of "show notifications" checkbox of app setting(find it by going setting->app manager->select my app from list). According to the value of this checkbox, different code will execute in my app.

Yours help is appreciated,

Thanking you.

Sangam_cs
  • 199
  • 2
  • 11
  • possible duplicate of [Design pattern for alternate action if app in foreground?](http://stackoverflow.com/questions/22001844/design-pattern-for-alternate-action-if-app-in-foreground) – ianhanniballake Mar 06 '14 at 04:52

2 Answers2

1

You can use the method onwindowFocusChanged() to determine whether your window (activity) gained (foreground) or lost (background) focus. This is the function:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);

//write your code here

}

You can add this to your activities

Sushil
  • 8,250
  • 3
  • 39
  • 71
0

public boolean IsAppForeground(Context context){

ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null){ return false; } final String packageName = context.getPackageName(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; } }return false; }

Lalit kumar
  • 2,377
  • 1
  • 22
  • 17