The example in this article describes how Android is programmed to determine whether an app is currently in the foreground. Share to everyone for your reference, specific as follows:
In the process to find information about the current app, to determine whether in the foreground run
private Boolean isapponforeground () {
Activitymanager Activitymanager = ( Activitymanager) Getapplicationcontext (). Getsystemservice (
context.activity_service);
String PackageName =getapplicationcontext (). Getpackagename ();
list<runningappprocessinfo>appprocesses = Activitymanager.getrunningappprocesses ();
if (appprocesses = null) return
false;
for (Runningappprocessinfo appprocess:appprocesses) {
if (appProcess.processName.equals (PackageName)
&& appprocess.importance = = runningappprocessinfo.importance_foreground) {return
true;
}
}
return false;
}
I hope this article will help you with the Android program.