How to be notified when foreground (top) activity (application) changes

前端 未结 2 1063
耶瑟儿~
耶瑟儿~ 2020-12-14 02:26

I want to write a service for Android platform that is notified when the current foreground activity changes. Basically the service should do some tasks only when the top ac

相关标签:
2条回答
  • 2020-12-14 02:48

    You should bind every activity to the service and you will know which activity is running.

    try this:

    List runningTaskInfos=actvityManager.getRunningTasks(1).get(i).topActivity .getPackageName();
    

    this method will give info of activity's package name which is in foreground..............

    0 讨论(0)
  • 2020-12-14 02:55

    AFAIK there are two ways to do that.

    1. Start a service and monitor the Activity Stack, you might check it here
    2. Use an Accessibility Service, you could find a solution here
    0 讨论(0)
提交回复
热议问题