Android: turn off display without destroying or pausing activity - java

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.

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.

Is there a way to tell android force stop activity which going to background without removing it from activity stack?

I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.

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.

What is the difference between activity visible liftime and foreground lifetime?

We know that the android activity cycle has multiple phases.
between onStart() and onStop() is called the visible lifetime
between onResume() and onPause() is called the foreground lifetime
What are the the key difference between them? Please give examples if possible.
If I display an Activity on the screen and the user is interacting with it, it is both in the foreground and visible.
If I start another Activity, which is transparent and shows a dialog box over the previous Activity, then the new Activity (the dialog box) is in the foreground and the old Activity is not in the foreground but still visible.
between onStart() and onStop() called visible lifetime that mean that the activity is visible either entire activity or partially visible and the user can see it on the screen and interacte with
between onResume() and onPause() called foreground lifetime that your activity is full visible and running and have full focus .
UPDATE
partially visible for example if another activity come in front of the current one and it only display a dialog and a transparent background . the user can see that activity but cant interact with it
Foreground Activity: You’d think what the user is currently interacting with would be the most important thing to keep alive.
Visible Activity: You’ll find that there are situations where your activity can be visible but not in the foreground. A simple example is when the foreground activity starts a new activity with Dialog theme or a translucent activity. Another example might when you invoke the runtime permission dialog.
Please find below link for better understanding
https://medium.com/androiddevelopers/who-lives-and-who-dies-process-priorities-on-android-cb151f39044f
The Visible Lifetime: Although this is termed the "visible lifetime", the app may not be directly visible and interacting with the user at any one time if it is not in the foreground. The feature that distinguishes this lifetime is that, even if not in the foreground, the app maintains resources such that it can instantaneously return to the foreground.
The Foreground Lifetime: During foreground lifetime the activity is in front of all other activities and interacting with the user.

Android Java: Understanding Activity Life?

I'm new to android development and I get how the android has an activity life.
If I have an app and I press a button to use the phone's camera funcationality like so...
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST);
}
How does onPause() or onDestroy() and the other stuff work?
I have this outside the onCreate()
protected void onPause(){
super.onPause();
}
If I want to press the back button or press the home button, do I have to destroy or pause the camera function? If so, I'm still trying to figure out how to do so?
Thanks!
When you start new activity from your current activity then there are two possibility of your current activity
Pause
Stop
Paused:
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped:
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
For example you are starting the Camera activity from your activity then your current activity will be Stop because the Camera activity will covers all your screen and your activity is not visible to camera activity.
Here is the complete description.
You are starting the Camera activity using the Intent so you do not have to handle the call back methods of the camera activity. System will manage the call back method you do not have to manage it.You just have to manage the Activity result which you will get in your activity from the Camera activity.
EDIT
And also ob course you never have to directly call any life cycle methods of the Activity.System automatically calls this method according to activity state.You just have to write your implementation in this methods to do your work.

Categories