Android如何知道某个应用已启动,并根据启动时间确定应用的优先级 [英] Android how to know an app has been started and range apps priority according the starting times

查看:50
本文介绍了Android如何知道某个应用已启动,并根据启动时间确定应用的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,人们如何知道某个应用已启动.我想检测启动时安装的所有应用,并根据其使用时间确定应用的优先级.有没有建议这样做的解决方案?

In Android, how does one know an app has been started. I want to detect all apps installed when it starts, and range apps' priority according to its using times. Is there a solution to suggest to do this?

我知道使用广播,但是启动应用程序后,ActivityManager是否会发送一些Intent,以及如何在代码中检测到此Intent?也欢迎任何其他解决方案.

I know using broadcast, but is there some Intent send out from ActivityManager when app had been started, and how to detect this Intent in code? Any other solution is welcome, too.

推荐答案

第一部分:

如果您知道应用程序的程序包名称,请尝试以下操作(在您的应用程序的onCreate方法中将以下代码片段放在该代码段中):

If you know the package name of your app, try this (put this following snippet in the onCreate method of your app):

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

然后

 boolean exit = false;
 while(!exit)
 { 
      List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
      ComponentName componentInfo = taskInfo.get(0).topActivity;
     if(componentInfo.getPackageName().equals("Your package name"))
     {
      //Do your work here
      exit = true;
     }
 }

启动应用程序时,它将被放入 componentInfo 中. taskInfo.get(0).topActivity 将在前台返回活动.因此,您可以通过使用第二个代码段比较程序包来知道您的应用已启动.

When you start your app, this will be put into componentInfo. The taskInfo.get(0).topActivity will return the activity in the foreground. Hence you can know that your app has been started by comparing package using the second code snippet.

注意:将第二个代码段放入 Asynctask 中,以便可以在后台中检查应用是否已启动

Note:Put this second code snippet in an Asynctask so that the checking of whether the app has started can be done in the background.

第二部分:

要获得优先级,我认为您可以通过查看列表 TaskInfo 来做到这一点,该列表将包含所有正在运行的应用程序.

To get the priorities, I think you can do it by checking the list TaskInfo which will contain all the running apps.

这篇关于Android如何知道某个应用已启动,并根据启动时间确定应用的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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