简体   繁体   English

如何知道打开了哪个应用程序?

[英]How to know which app is opened?

I want to know when any app is opened.我想知道什么时候打开任何应用程序。 Clearly to say, i am making service that blocks some apps ( according to user ) and i want to check app is this similar to any app in the blocklist and abort that app process.很明显,我正在提供阻止某些应用程序的服务(根据用户),我想检查应用程序是否与阻止列表中的任何应用程序相似,并中止该应用程序进程。 And does it need root access?它需要root权限吗?

You are able to know which apps are currently opened您可以知道当前打开了哪些应用程序

ActivityManager manager = 
    (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();

Now from the processes find your app and write the logic as per your requirement.现在从processes找到您的应用程序并根据您的要求编写逻辑。

Use this code使用此代码

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runtasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < runtasks.size(); i++) {
Log.d("Running task", "Running task: " + runtasks.get(i).baseActivity.toShortString() + "\t\t ID: " + runtasks.get(i).id);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM