Проверка открыта ли какая-либо деятельность или нет.
/***
* Checking Whether any Activity of Application is running or not
* @param context
* @return
*/
public boolean isForeground(Context context) {
// Get the Activity Manager
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// Get a list of running tasks, we are only interested in the last one,
// the top most so we give a 1 as parameter so we only get the topmost.
List<ActivityManager.RunningTaskInfo> task = manager.getRunningTasks(1);
// Get the info we need for comparison.
ComponentName componentInfo = task.get(0).topActivity;
// Check if it matches our package name.
if(componentInfo.getPackageName().equals(context.getPackageName()))
return true;
// If not then our app is not on the foreground.
return false;
}
Используйте это как:
if(isForeground(context)) {
//Nothing to do
} else {
/**
You can do here what you want to do
**/
}