Android how to know an app has been started and range apps priority according the starting times

前端 未结 1 1299
长情又很酷
长情又很酷 2020-12-06 22:48

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

相关标签:
1条回答
  • 2020-12-06 23:12

    First part:

    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); 
    

    Then,

     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;
         }
     }
    

    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.

    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.

    Second part:

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

    0 讨论(0)
提交回复
热议问题