I am doing a status bar notification in my android app that is triggered by c2dm. I don't want to display the notification if the app is running. How do you determine if the app is running and is in the foreground?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Here's the code for nice simple solution described above by @user2690455 . Although it looks a bit verbose, you'll see overall it's actually quite light-weight
In my case we also use AppCompatActivity, so I had to have 2 base classes.
I found a more simpler and accurate way to check if the application is in foreground or background by mapping the activities to boolean.
Check the complete gist here
Alternately, you can check with the
ActivityManager
what tasks are running bygetRunningTasks
method. Then check with the first task(task in the foreground) in the returned List of tasks, if it is your task.Here is the code example:
And don't forget to add the
GET_TASKS
permission in the manifest.xml file in order to be able to rungetRunningTasks()
method in the above code:p/s : If agree this way, please to note that this permission now is deprecated.
This is a pretty old post but still quite relevant. The above accepted solution may work but is wrong. As Dianne Hackborn wrote:
These APIs are not there for applications to base their UI flow on, but to do things like show the user the running apps, or a task manager, or such.
Yes there is a list kept in memory for these things. However, it is off in another process, managed by threads running separately from yours, and not something you can count on (a) seeing in time to make the correct decision or (b) have a consistent picture by the time you return. Plus the decision about what the "next" activity to go to is always done at the point where the switch is to happen, and it is not until that exact point (where the activity state is briefly locked down to do the switch) that we actually know for such what the next thing will be.
And the implementation and global behavior here is not guaranteed to remain the same in the future.
The correct solution is to implement : ActivityLifeCycleCallbacks.
This basically needs an Application Class and the handler can be set in there to identify the state of your activities in the app.
FYI, if you use Gadenkan solution (which is great!!) don't forget to add
to the manifest.
The previous approaches mentioned here are not optimal. The task based approach requires a permission that might not be desired and "Boolean" approach is prone to concurrent modification mess ups.
The approach I use and which (I believe) works quite well in most cases:
Have a "MainApplication" class to track activity count in AtomicInteger:
And create a base Activity class that other activities would extend: