如何检查,如果用户正在运行,并且在Android上使用的应用程序? [英] How to check if a user is currently running and using an app on android?

查看:208
本文介绍了如何检查,如果用户正在运行,并且在Android上使用的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用Android来检测,如果在Android手机上有一定的应用程序是在屏幕上打开。例如,检查以查看是否用户当前正在使用他/她的照片的应用程式。大部分我读过迄今为止的检查是否有一定背景的应用程序正在运行的问题;我想弄清楚我是否可以检查是否有一定的应用程序是当前打开并在屏幕上运行。因此,使用我的应用程序(这会在后台运行),以检测是否一个​​应用程序,例如照片应用程序,是开放的,正在使用的用户。


解决方案

  ActivityManager AM =(ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
清单< ActivityManager.RunningTaskInfo>任务= am.getRunningTasks(1);
ActivityManager.RunningTaskInfo任务= tasks.get(0); //当前任务
组件名rootActivity = task.baseActivity;
串currentPackageName = rootActivity.getPackageName();
如果(currentPackageName.equals(com.sec.android.gallery3d)){
     //做任何这里
}

使用currentPackageName,看它是否是你想要的应用程序。

有关更多信息,请看这里:<一href=\"http://stackoverflow.com/questions/11118721/android-how-can-i-get-current-opened-application-name-on-screen\">Android:我怎样才能获得当前屏幕上

打开的应用程序名称

编辑:
此外,你应该添加此权限

 &LT;使用许可权的android:NAME =android.permission.KILL_BACKGROUND_PROCESSES/&GT;

Is it possible to use android to detect if a certain app on an android phone is open on the screen. For example, checking to see if the user is currently using his/her photo's app. Most of the questions I've read so far are checking the whether a certain BACKGROUND app is running; I'm trying to figure out whether I can check if a certain app is currently open and running on the screen. So using my app (which would be running in the background) to detect whether an app, for example the photos app, is open and being used by the user.

解决方案

ActivityManager am =(ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ActivityManager.RunningTaskInfo task = tasks.get(0); // current task
ComponentName rootActivity = task.baseActivity;


String currentPackageName = rootActivity.getPackageName();
if(currentPackageName.equals("com.sec.android.gallery3d")) {
     //Do whatever here
}

Use currentPackageName to see if it is the application you want.

For More Info, Look Here: Android: How can I get current opened application name on screen

EDIT: Also, you should add this permission

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

这篇关于如何检查,如果用户正在运行,并且在Android上使用的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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