@Override
protected void OnStop () {
if (!isapponforeground ()) {
debug.i ("Dwy", "Enter Background");
Misbackground = true;
} else {
debug.i ("Dwy", "foreground");
Misbackground = false;
Judge is app. background when onStop () get called.
public Boolean isapponforeground () {
//Returns A list of application processes this are running on the
//device< C3/>activitymanager Activitymanager = (Activitymanager) getapplicationcontext (). Getsystemservice (Context.ACTIVITY_ SERVICE);
String PackageName = Getapplicationcontext (). Getpackagename ();
list<activitymanager.runningappprocessinfo> appprocesses = Activitymanager
. getRunningAppProcesses ();
if (appprocesses = null) return
false;
for (Activitymanager.runningappprocessinfo appprocess:appprocesses) {
//The name of the process, this object I s associated with.
if (AppProcess.processName.equals (packagename)
&& appprocess.importance = = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {return
true;
}
}
return false;
}
Method Two:
/**
* Need permission: Android.permission.GET_TASKS
* *
@param context
* @return/Public
Boolean Isapplicationbroughttobackground {
Activitymanager am = (activitymanager) context
. Getsystemservice (context.activity_service);
list<runningtaskinfo> tasks = am.getrunningtasks (1);
if (Tasks!= null &&!tasks.isempty ()) {
ComponentName topactivity = tasks.get (0). topactivity;
DEBUG.I (TAG, "topactivity:" + topactivity.flattentostring ());
DEBUG.F (TAG, "topactivity:" + topactivity.flattentostring ());
if (!topactivity.getpackagename (). Equals (Context.getpackagename ()) {return
true;
}
}
return false;
}