Detect 'home button pressed' event in android service displaying a UI (similar to facebook chatheads)

前端 未结 8 1281
暗喜
暗喜 2020-12-13 09:35

In facebook chatheads, that are part of the facebook messenger app, I noticed the following behavior: As far as I can see, the chat head itself and the opened chat screen ar

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 10:09

    You can monitor (e.g. check every 200 ms) the top running activity, and see if it is your activity or some other activity, and know when it changes. This will also let you handle cases like an incoming call.

    ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
    am.getRunningTasks(1).get(0).topActivity...
    

提交回复
热议问题