获取当前运行的应用程序应用程序

由网友(雷鸣八千里)分享简介:我如何可以获取当前运行的应用程序? How may I get the current running application? 我要检查,如果应用程序在服务的情况。我用下面的code表示。I want to check if the application is running or not in servi...

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

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" />
阅读全文

相关推荐

最新文章