如何区分后台应用程序和 Android 关闭的应用程序?
我正在尝试找到一种方法来判断应用程序是否已关闭或已进入后台。我需要能够对任何应用程序执行此操作。我正在使用 AcitivityManager 来轮询哪些应用程序正在运行,并将它们与我正在查找的应用程序进行比较。问题是,当它实际上在后台并且当我使用后退按钮“关闭”应用程序时,重要性级别被设置为 IMPORTANCE_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;
}
}
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 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您点击后退按钮时,Android 中的应用程序不会“关闭”。返回按钮调用活动堆栈上的前一个活动并将当前活动置于后台。
因此,主页按钮会清除活动堆栈并调用启动器应用程序,后退按钮会调用堆栈中的上一个活动。两者都将当前应用程序置于后台。
仅当系统资源不足时,Android 上的应用程序才会关闭,即被杀死并从内存中删除。
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.
当您使用后退按钮从应用程序切换回桌面时,应用程序将进入后台。它没有关闭。
如果需要,Android 会稍后关闭它,并且这不会是用户操作的结果。
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.