I am trying to find a way to tell if an application has been closed or has went to the background. I need the ability to do this with any application. I am using the AcitivityManager to poll which applications are running, and compare them to an application I am looking for. The problem is that the importance level gets set to IMPORTANCE_BACKGROUND when it is actually in the background AND开发者_JAVA技巧 when I use the back button to "close" the app. How can I tell if it is actually closed or in the background?
private boolean isActivityRunning(){
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
Log.d(TAG, "Polling Serivce at application " +procInfos.get(i).processName);
if(procInfos.get(i).processName.equals(appLaunchedPackage)&&
(procInfos.get(i).importance ==RunningAppProcessInfo.IMPORTANCE_VISIBLE ||
procInfos.get(i).importance ==RunningAppProcessInfo.IMPORTANCE_FOREGROUND ||
procInfos.get(i).importance ==RunningAppProcessInfo.IMPORTANCE_BACKGROUND) ) {
Log.d(TAG, "Polling Serivce - the app has been found!");
return true;
}
}
App in android in not "closed" when you hit back button. Back button invokes previous Activity on activity stack and puts current one in the background.
So home button clears the activity stack and invokes launcher app, back button invokes previous activity from stack. Both put current app in background.
Apps on android are only closed, i.e. killed and removed from memory, when system is low on resources.
When you use the back button to switch back from your app to the desktop, the application goes background. It is not closed.
Android will close it later, if needed, and this will not be the result of a user action.
精彩评论