Reputation: 199
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.
Upvotes: 2
Views: 862
Reputation: 2485
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; }
Upvotes: 0
Reputation: 8488
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
Upvotes: 1