Pausing the Activity in Android - android

I have developed a game in android and it has some timer in it,when ever I'm playing the game if some one rings me the game is not going to paused state the time is still running in the background.what i need is when some in coming call has arived it should display a prompt message that the game is in pause state how to achieve this..?

You will have to override the onPause() function which is called everytime you get called you go back to the main menu or whatever. Here is a sample of the docs :
When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state.
I advise you to read this link carefully to understand the different steps in an activity life and that link as well
Here is how to override your onPause() function :
#Override
protected void onPause() {
//Do something like pausing the timer
}
When the user goes back to the game, you want the timer to restart and therefore, you'll override the onResume() function :
#Override
protected void onResume() {
//Resume the timer
}
Have a good day !

Related

Android app timeout how to apply? when it minimized

I want to make a exam app I want to apply a feature in my app in this feature when student minimize app then exam will automatically cancelled. so they can't cheat so please tell me what will and how will I do this in Android app in adt.
In your exam activity, override onPause() and paste cancel exam method before super.onPause().
Edit: I think you need onPause() instead of onStop(). Learn more about Activity Life cycle
When you minimize app, onStop will called.
Called when you are no longer visible to the user
So inside onStop you can cancel the example
#Override
protected void onStop() {
super.onStop();
// cancel exam here
}

Dealing with Android's MediaPlayer while rotating screen, pressing Home Button or App is interrupted

I'm in a tricky part of the workflow/design on my app.
I have a MediaPlayer that runs into a Service. I want the music to stop when user leaves (BUT NOT CLOSES) the App. That is, the Home Button.
So I implemented MediaPlayer's pause() and start() methods into Activity's onStop() and onResume(), respectively. That is working fine when testing with the button and relauching the App, but it also happens when the screen is rotated due to the Activity's lifecycle.
So, how can I avoid this in a efficient / elegant way? The music mustn't be interrupted when device is rotated. I thought in overriding Home Button's click method, pause there the MediaPlayer and setting a global boolean flag to check in onResume() if MediaPlayer must be resumed. But this seems to me like a hack or so, not a good practice.
I'm wrong with this? Is there a better way?
Any advice is appreciated.
There are a few options here:
If your minSdkVersion is at least 14, you can check the isChangingConfigurations() flag before stopping the media player:
#Override
protected void onStop() {
if (!isChangingConfigurations()) {
// Stop the Media Player
}
}
Otherwise, you could watch for onUserLeaveHint():
#Override
protected void onUserLeaveHint() {
// Stop the Media Player
}
Although that won't be called if another application is forcibly drawn to the foreground (e.g. a phone call comes in).
EDIT: As an alternative you might be able to rely on onWindowFocusChanged(). It is called with false when leaving the activity with Home or Back, but not on a configuration change. It should also be called when another activity is brought to front.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// Stop the MediaPlayer
}
As a side note, you should typically use symmetric lifecycle methods (e.g. instead of onPause(), onStart() use either onPause()/onResume() or onStop()/onStart())

Is there a way to know why my Activity is being paused?

I have an app sort of like a browser, so I don't control the javascript that runs inside the pages, some of it might be looping like crazy and wasting battery. Also video or audio might be playing. I want all that to pause when the user leaves my app for another app. To do this I call webView.onPause() and webView.pauseTimers(). I tried only doing onPause but depending on what pages I had loaded my battery would still run down. As you might know, pauseTimers applies to all webviews so that means any ads I might have stop loading.
So for this reason I want to call pauseTimers only when my activity is being paused because the user is leaving the app, but not when the user is leaving my activity to go to another activity in my same app, because it might have ads.
So can I know when my activity is being paused because the user is leaving the app?
Thanks.
You should override Activity.onUserLeaveHint()
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
Have you tried re-implementing the onPause() method? You can analyze the stacktrace in order to get some clues.
Also, this link might help you:
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
Override the onPause method in your actvity.
#Override
protected void onPause() {
super.onPause();
//Do whatever you want to do
}

Thread Continues to Execute After Closing the Application

I have an android application that uses Threads. Application waits for some time, then executes a function.
Things go pretty well if user waits for some time. After the predefined time t ends, the function gets executed.
However, if the user clicks on back button of the device and return to main screen, after the time t ends, the application appears again.
How can I understand if the user pressed back, or closed my application? How can I stop the thread and release everything if I get the leaving message -let's say USER_EXITED?
in your activity when the activity is going to end you can check if it is finishing like this and take care of things to do with your threads
#Override
public void onPause() {
if(isFinishing()){
//put the correct checks or shutdowns
{
super.onPause();
}
I think that you need to create a Service.
http://developer.android.com/reference/android/app/Service.html

Android -- How to properly handle onPause/onResume methods?

I have an app that starts playing sounds and begins/resumes gameplay in the onResume() method, but what I'm noticing is that if my app was the last run application when I put the phone into standby (screen off), and I just press the Menu button to check the time, then the phone starts playing the game and sounds in the background (the app isn't actually visible, only the screen with the date/time is, yet onResume must have been called in my app). What am I to do here? Is there a way to discern what is reactivating the app, and then add a conditional statement that only starts the game when the app is actually visible?
Here is a snippet from my onResume:
#Override
protected void onResume()
{
mySaveGame = Utilities.loadSavegame(this);
//check the savegame
if(mySaveGame!=null)
{
//start game using savegame values
this.startFromSavedGame(mySaveGame.getIsLevelComplete());
}
else
{
//run the 1st-run components
this.startFirstRun();
}
super.onResume();
}
The only thing I can think of doing to prevent the game from starting whenever the screen gets turned on (even when the app isn't visible) is to put this.finish() as the last line in onPause()... but that forces you to restart the app every time you want to go back to it because the precess itself was killed (which is fine because my onPause saves persistent data, but it's not an elegant solution).
Please help.
Have you considered switching to onStart() and onStop(), rather than onResume() and onPause()?
I was having the same problem (I had my music player resume/pause at onResume()/onPause()) and the best solution I found is to pause and resume my activity when it is on the foreground which you can get with public void onWindowFocusChanged (boolean hasFocus) callback.
Edit: This in an old and slightly incorrect answer - correct response is described in the Android Developers Blog: making android games that play nice

Categories

Resources