Android: Perform Small Action When App Leaves Foreground - java

I am building a timer application and would like to pause it whenever the user removes the app from the foreground (such as via the Back or Home buttons). What would be the easiest way to do this? I'm thinking onStop() would be, but I have never done this before, so I am not entirely sure.

You can do this by overriding onPause() (or onUserLeaveHint() if you only want user actions.)

Related

How to monitor if my App is paused or resumed?

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.

Refresh of screens on backgrounding and foregrounding an application

I am working on an Android app with multiple activities. When moving from one activity to another in certain cases I want to refresh the display but not in others.
One case is where I background the application and foreground it again. When I foreground it, I want to refresh everything on the screen depending on which activity
I backgrounded to begin with. How can I do this? I am unfortunately a bit new to Android so some appropriate basics where applicable would also be helpful.
http://developer.android.com/guide/components/activities.html#SavingActivityState
You can use onSaveInstanceState() to save information before onPause() is called. In onResume() you can use the saved info as a case in a switch statement or some conditional to refresh what you want.

The sound stopped when press lock button

I had the Android App which play sound when specific times , I did my
code well but when I locked the screen the sound isnot play , I
checked my code and I find that the code that I added when user press
lock button is the reason of the problem .How to solve this issue ?
#Override
protected void onPause() {
Player.stopAzan();
finish();
super.onPause();
}
Maybe try using a service? Services are basically same that activities but they run in background and have no content view if I get them right :D
http://www.vogella.com/tutorials/AndroidServices/article.html
What you are trying to achieve is impossible since when the screen is locked, the Activity is stopped.
You either decide if want to play in background or not, because when the screen is locked, the Activity goes background.
If you don't need to play in background then you're good to go, just remove the finish() method.
If you do need to play the music in background, use a Service to start and stop player based on Intents passed from Activity user controls.

Android screensaver in app if it's not used

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!!

Calling onCreate() from locked screen

I am developing an android emergency app and I want to add a feature that can call a certain method from my activity class when the screen is locked. Is there anyway for my app to detect any kind gestures like shake, screen tap pattern or any kind of movement when the screen is locked?
Not sure if it will work, but I think you can put a listener in a service and put the servicee in the foreground. I have no clue what exactly youwant to achieve but this is what I can think of right now. BTW -- pass information from a service the foreground activity

Categories