This question already has answers here:
Checking if an Android application is running in the background
(35 answers)
Closed 8 years ago.
I have a countdown timer in my game and I have to cancel it once the home button is pressed. I tried with this:
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
timer.cancel();
}
But this does not work for me, cause I have some popup activities and some other stuff because of which I lose the focus of my main activity and that makes the timer to stop, and that's not good for me. Is there a way to stop my timer ONLY if the home button is pressed?
try this
#Override
public void onStop() {
super.onStop();
timer.cancel();
}
This is a common type of question, with no trivial, built-in solution. But easy enough to write your own.
The basic solution relies on monitoring the onPause() and onResume() events of all your activities. This would normally be done by using a base Activity class that implements the below behaviour, and all your activities extend it.
I normally use my Application class to handle the logic of knowing when it is no longer in the foreground. So in the application class, I have methods that get called every time my activity pauses, or resumes.
when paused:
I start a short timer, say 300 milliseconds long
when resumed:
I stop that timer, if it is running
So:
if you launch your activity B, your Application class will get a pause signal, followed right away by a resume signal. Your timer will not expire.
if your application is sent to the background, you will get a pause signal, and no resume signal. Your timer WILL expire.
When the timer expires, you know that your application is in the background, and you take action. In your case, that means cancelling the game timer. Other people will log out of a server. In my case, I turn NIGHT MODE off.
You can base the code on my answer to a similar question, which shows how each activity would alert the application of its status:
How to know in BroadcastReceiver if App is running on foreground?
In that question you can see a boolean flag is being set. In your case, you would start or stop the timer.
For more details, check out these related links:
Android auto-logout when app goes to background
Run code when Android app is closed/sent to background
Checking if an Android application is running in the background
Maybe you should stop your timer when the activity's onPause or onStop method is called.
Related
Is Service and Notification the correct way to implement a background app with the following behavior?
1- User open the app, make some configurations and touch in a "run" button;
2- The main activity must be closed and a background service will be started;
3- A fixed notification will be displayed with some buttons ("stop" to finish the service and "Reconfigure");
thank you
I did an app that at short intervals (1 to 10 sec) acquires constantly data from a remote TCP server, even on the background also with the screen off.
In the Main Activity onStop I acquire a Partial WakeLock and I start the Service with STARTFOREGROUND_ACTION intent, the Service calls startForeground and shows the Notification.
On Activity onResume the WakeLock is released, the Service stops with STOPFOREGROUND_ACTION intent and the Service itself calls stopForeground & stopSelf.
I don't have buttons in the notification, if user touches the notification the Activity is shown.
It works very well I tested it with hours of continuous operation.
i have a mediaplayer in an activity, i want to implement a feature in which when a user clicks a button, the screen display goes off, but the video continues to play in that activity.
Acording to the life cycle of the Android activity, when an activity becomes background, onPause is called, however, from now on, the system can kill the activity by its will, ie. the system requires more memory.
It'S the system that decides to kill the activity or not, not the developers. In general, It'S because more memory are needed.
Understand the Lifecycle Callbacks
You need to implement a Service. Start at this link on how to do it: https://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
activity should be paused
you can override the procedure onPause() and onResume() to check the event
when pause, you pause the media, when resume you resume the media
the media is playing the other thread and draw the activity. when the activity is pause, but the media thread is working.
I have an Android app where I want to track when the app is paused or resumed.
paused: User pressed the home button and the app is still running in the background.
resumed: app runs in background and user opens the app.
How can I being notified when my app was paused/resumed?
paused: User pressed the home button and the app is still running in the background.
I am going to guess that the initial state is that one of your activities was in the foreground at the time the HOME button was pressed.
On the whole, there is no notion in Android of an "app" being in the foreground or the background, though we sometimes use that phrasing as shorthand for other scenarios.
Whatever activity was in the foreground will be called with onPause() and onStop() when the user presses HOME, but those events are also called in many other scenarios (e.g., user presses BACK). onUserLeaveHint() will be called when the user presses HOME but not BACK, but onUserLeaveHint() is not called in other scenarios (e.g., incoming call screen takes over the foreground). Whether onUserLeaveHint() will meet your requirements, I cannot say.
resumed: app runs in background and user opens the app.
onStart() and onResume(), at minimum, will be called on your activity that takes over the foreground. Those will be called at other times too, such as when the activity is coming onto the screen for the first time. There is also onRestart(), which will be called only if the activity is being started after having been stopped (i.e., after a prior onStop() call), which will weed out the newly-created-activity scenario. However, onRestart() will be called in other scenarios as well, such as part of a configuration change (e.g., screen rotation).
In general, what you are seeking is not really part of the Android architecture. You may need to approach your problem in some other way.
In you Android activity you can override the onPause and onResume methods.
See the documentation on Lifecycle Callbacks for a list of other lifecycle callbacks that you can implement.
So I've looked up similar problems to this, and have followed the advice in those threads, but it seems to give me no change in behavior.
So I'm writing an app that essentially notifies the user when they're going too fast or too slow based on GPS onLocationChanged() calls. I overrided the onBackPressed() method to finish() and return but the activity continues to run in the background when I go back to the main activity.
To be clear, I DO want the activity to run when the app is minimized or screen is off. I only want it to stop when the user goes back to the menu (IE, hits the back button)
Here's the link to the class on pastebin:
http://pastebin.com/V7z5c3HH
Thanks for your help! =D
Unsubscribe your location listener in the onDestroy method.
However, what you need for your GPS processing is probably a Service, not an Activity.
You need to remove the updates for that listener.
#Override
protected void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
I created an android app. The screen never turns off:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Now I want to show a picture if the screen is not pressed for 5 minutes or something else. The app should not be closed, when pressing on the image the app should be open.
How can I realize that?
I would discourage you from taking this approach. Users expect to have a consistent user experience between various apps on their devices, and likely have a preference to how their device sleeps, either by having specified a sleep timeout or displaying a daydream as introduced in Android 4.2.
If you'd like to provide users with the option to display a screensaver associated with your app, I suggest including a Daydream in your app and otherwise acknowledging the user's preferences.
That being said, if you cannot use Daydream, you could observe if the app is being used or not. Two things come to mind:
Have the root view of your activity intercept touch events to observe if any of its children have been touched.
Observe the activity's onPause() and onResume() to acknowledge that the activity is still being displayed.
You could then invoke a Runnable by posting it to a view using postDelayed(Runnable action, long delayMillis), being wary to remove it when the activity is paused or the timer should be reset using removeCallbacks(Runnable).
I solved the problem!!!
I used that event:
public boolean dispatchTouchEvent(MotionEvent ev)
{
super.dispatchTouchEvent(ev);
// cancel my Timer
return true;
}
Thanks!!