0

Is there any way the android system can inform a service that an activity or a task in the system(not only my activities/tasks!) has somehow changed (e.g. another activity is brought to the front or is stopped)? This is to avoid polling the same information, of course.

There is an identical question here, but it was asked 3 years ago, so in the meantime, maybe someone has an answer to this? As a supplement to this question: If the answer is no, is there any way someone could enhance this feature by, let's say, low-level programming or something?

Community
  • 1
  • 1
Chris S.
  • 13
  • 4

3 Answers3

0

Isn't what you want covered already by the activity lifecycle events?

Aren't onPause() and onResume() sufficient?

amahfouz
  • 2,328
  • 2
  • 16
  • 21
  • Yes, if I was **only** interesed in **my activity**. I'm interested in the overall system and if **any** activity changes. Please read the question more carfully/ refer to the linked question. The answers there have the same problem as yours. – Chris S. Jun 30 '15 at 22:13
0

No, there is no general solution. You can only record this information in the lifecycle methods of your own activities. And starting in Android 5, even polling does not work. ActivityManager#getRecentTasks() was nerfed. From the docs:

This method was deprecated in API level 21. As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak personal information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks (though see getAppTasks() for the correct supported way to retrieve that information), and possibly some other tasks such as home that are known to not be sensitive.

Since this is considered a security issue, you can bet that if there is a low-level way to do it, Google will eventually find out and break it.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • Yes, I'm aware of this API changes. One could use UsageStats from Lollipop upwards. – Chris S. Jun 30 '15 at 22:18
  • As for the security thing: We are talking about an innocent notification if an activity or task has changed *somehow*, not even how. I don't see how this information could possibly be harmful. You just know enough for your service to wake up and see what's going on. – Chris S. Jun 30 '15 at 22:24
  • @C.S. Maybe. But `UsageStats` requires a hidden permission that can only be granted via a system activity, and it's documented that the system activity may not be present on all devices. Notably, Samsung has removed it from their Lollipop devices. So if you base your solution on the `UsageStats` API, you're choosing not to support Samsung devices. – Kevin Krumwiede Jun 30 '15 at 22:31
  • I didn't know Samsung removed this permission, lol. Well, essentially there is no supported way of knowing which activity is currently in the foreground? Well then, we need some clever hack here ;-) – Chris S. Jun 30 '15 at 22:38
  • @C.S. Apparently LG also removed it. As for the security thing, Google's philosophy seems to be that users are idiots and Android devices will immediately be taken over by malware if they're allowed to do anything remotely interesting. This is why Android will never be a general-purpose computing platform. That and Google hates developers, as evidenced by their recent rage-closing of 11,000 bug reports, etc. – Kevin Krumwiede Jun 30 '15 at 22:40
  • I should have started developing android years earlier, when there weren't such silly restrictions. It is exciting to try and find a "hole" in the system, though. But I'm afraid that by the time I found it, there is no android anymore. – Chris S. Jun 30 '15 at 22:45
  • @C.S. I just learned about a new hack that people are using on Lollipop. See [this answer](http://stackoverflow.com/a/27140347/1953590). But it's already been broken on M. – Kevin Krumwiede Jul 02 '15 at 03:40
  • Well, that is not really a hack.I found this method myself and tested it, but as also some comments suggest, the importance_foreground won't give you the top activity. UsageStats seem to be the only way left to obtain that information. I really hope Samsung and LG did not remove the required permission from their devices. – Chris S. Jul 02 '15 at 20:26
  • @C.S. By "hack" I mean unintended usage of the API, which its removal seems to suggest it was. – Kevin Krumwiede Jul 02 '15 at 20:46
  • Why is there a constant IMPORTANCE_FOREGROUND if it is not intended for any use? Never mind :-) If nobody is going to answer this question further, I will accept your answer, as it seems to be the (sad) truth. – Chris S. Jul 02 '15 at 21:39
0

i am feeling lazy sorry, try this logic.

check this post. what you would want to achieve here is record all the Tasks running currently; and in a loop you keep checking, you will be notified when a task die by checking the overall running Task, loop through and you will know who died- but this is limited not all versions, and some people will tell you, it is not meant for that.

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • I appreciate the answer but can you guys actually read the question before answering? ^^ I clearly said I want to *avoid* polling. I know how to do it this way, that's not the problematic part. But since this method involves checking yourself for updates regularly, it is not what I wanted. – Chris S. Jul 01 '15 at 10:06
  • i am sorry Sir for not paying attention @C.S. but handling global Task requires much input, because as to my knowledge android does not want you to handle it, but itself, so to get there you need to penetrate much deeper, but hey my thoughts are just my thoughts. – Elltz Jul 01 '15 at 22:48