获取当前运行的应用程序 [英] Getting current running application

查看:180
本文介绍了获取当前运行的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以获取当前运行的应用程序?

How may I get the current running application?

我要检查,如果应用程序在服务的情况。
我用下面的code表示。

I want to check if the application is running or not in service. I used the following code for that.

       ActivityManager activityManager =
       (ActivityManager)
       this.getSystemService(ACTIVITY_SERVICE );

   PackageManager pm =getPackageManager();
   procInfos = activityManager.getRunningAppProcesses();

但它返回我的几个应用程序列表,即使我pressed后退按钮的主屏幕上。正在运行的应用程序列表仍显示他们在列表中。但我想该列表仅显示在前台的应用程序。

But it is returning me list of several applications, even if I pressed back button on their home screen. The running app list is still showing them in the list. But I want that list to show only the apps in foreground.

那么,如何前台和后台正在运行的应用区别?

So, how to differentiate between foreground and background running apps?

或者换句话说,如何检测当前前台运行的应用程序,或其中不被破坏。我不想让后台运行的应用程序。

Or in other words, how to detect current foreground running applications, or which are not destroyed. I don't want to get background running apps.

推荐答案

您可以在c它给当前活动画面的包试试这个$ C $:

You can try this code it gives the package of the currently active screen:

ActivityManager am = (ActivityManager) this
                .getSystemService(ACTIVITY_SERVICE);

// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("your.package.name"))
{
    //Do your stuff
}

您可能需要允许

<uses-permission android:name="android.permission.GET_TASKS" />

这篇关于获取当前运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆