java - Either show notification or react in application -
i have background service doing work - retrieving user location interval (launched startservice
). special condition reached i'd following:
- if application in foreground start specific activity.
- if application not in foreground or closed show notification start required activity on tap.
i know how show notification , how handle intent server broadcast receiver example. how can determint if application in foreground? or may can suggest complete better solution?
i determint if application in foreground
there's couple of ways find out in front, prefer track myself (as helps me apply additional logic if needed). plus it's pretty simple task. make happen need static int
based counter somewhere (you can use application
object if have one, or have elsewhere, not matter). in each activity
's onresume()
increment counter one, , in onpause()
decrement one. if counter equals 0
none of activities in foreground perspective in background , post notification. simplicity in activitybase
class activities extend.
if not want track yourself, can use activitymanager
see what's in foreground:
public boolean isappinforeground() { activitymanager = (activitymanager)getsystemservice(context.activity_service); list<runningtaskinfo> services = am.getrunningtasks(integer.max_value); return (services.get(0).topactivity.getpackagename().tostring() .equalsignorecase(getpackagename().tostring())); }
but requires <uses-permission android:name="android.permission.get_tasks" />
entry in manifest, not welcome.
Comments
Post a Comment