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
Make a global variable like
private boolean mIsInForegroundMode;
and assign afalse
value inonPause()
and atrue
value inonResume()
.Sample code:
This is useful only when you want to perform some action just when your activity starts and its where you want to check if app is in foreground or background.
Instead of using Activity manager there is a simple trick which you can do through code. If you observe the activity cycle closely, the flow between two activities and foreground to background is as follows. Suppose A and B are two activities.
When transition from A to B: 1. onPause() of A is called 2. onResume() of B is called 3. onStop() of A is called when B is fully resumed
When app goes into background: 1. onPause() of A is called 2. onStop() of A is called
You can detect your background event by simply putting a flag in activity.
Make an abstract activity and extend it from your other activities, so that you wont have to copy paste the code for all other activities wherever you need background event.
In abstract activity create flag isAppInBackground.
In onCreate() method: isAppInBackground = false;
In onPause() method: isAppInBackground = false;
In onStop() method: isAppInBackground = true;
You just to need to check in your onResume() if isAppInBackground is true. n after you check your flag then again set isAppInBackground = false
For transition between two activities since onSTop() of first will always called after second actvity resumes, flag will never be true and when app is in background, onStop() of activity will be called immediately after onPause and hence the flag will be true when you open the app later on.
There is one more scenario though in this approach. If any of your app screen is already open and you put the mobile idle then after some time mobile will go into sleep mode and when you unlock mobile, it will be treated at background event.
Slightly cleaned up version of Gadenkan's solution. Put it any Activity, or maybe a base class for all your Activities.
To be able to call
getRunningTasks()
, you need to add this in yourAndroidManifest.xml
:Do note what
ActivityManager.getRunningTasks()
Javadoc says though:Update (Feb 2015)
Note that
getRunningTasks()
was deprecated in API level 21!So what I wrote earlier is even more relevant:
In many cases you can probably come up with a better solution. For example, doing something in
onPause()
andonResume()
, perhaps in a BaseActivity for all your Activities.(In our case we didn't want an offline alert activity to be launched if we are not in the foreground, so in BaseActivity
onPause()
we simply unsubscribe from the RxJavaSubscription
listening for "went offline" signal.)I would like to add that a safer way to do this - than checking if your app is in the background before creating a notification - is to just disable and enable the Broadcast Receiver onPause() and onResume() respectively.
This method gives you more control in the actual application logic and is not likely to change in the future.
Based on the various answers and comments, here is a more inlined version that you can add to a helper class:
As mentioned in other answers you need to add the following permission to your
AndroidManifest.xml
.