For my android app, I want to keep the screen from dimming while certain activities (e.g. CatActivity) are in the resume state. Does anyone know how to do that? is there a way to declare so in the manifest or in the activity itself?
UPDATE
I found the answer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Is there a way to turn it off? I don't see a removeFlags method.
To delete the flag simply call the clearFlags() method:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Related
I have a ProgressDialog running, and I don't want that the screen goes off while this ProgressDialog is running. What should I use? Should I use a Windows Feature, or something like that?
I found the solution.
You should use something like that:
progressDialog.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
That worked for me.
type the following line oncreate method
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Certain apps need to keep the screen turned on, such as games or movie apps. The best way to do this is to use the FLAG_KEEP_SCREEN_ON in your activity (and only in an activity, never in a service or other app component). For example:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
The advantage of this approach is that unlike wake locks (discussed in Keep the CPU On), it doesn't require special permission, and the platform correctly manages the user moving between applications, without your app needing to worry about releasing unused resources.
Another way to implement this is in your application's layout XML file, by using the android:keepScreenOn attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
...
</RelativeLayout>
Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON. You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.
Note: You don't need to clear the FLAG_KEEP_SCREEN_ON flag unless you
no longer want the screen to stay on in your running application (for
example, if you want the screen to time out after a certain period of
inactivity). The window manager takes care of ensuring that the right
things happen when the app goes into the background or returns to the
foreground. But if you want to explicitly clear the flag and thereby
allow the screen to turn off again, use clearFlags():
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON).
See more at: http://developer.android.com/training/scheduling/wakelock.html#screen
[SOLVED]
i got a question... When i lock my phone while my app is running, it changes automatically to the main activity (the one which is started at the beginning) when in unlock it.
Anybody knows how to handle that the activity at the moment of locking stays?
Thanks!
Does your activity call finish() in its onPause() method?If so remove it
I use a MediaPlayer to play an MP3. Currently I disabled screen orientation changes by using
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
in the manifest. I do want to support landscape mode now - i.e. removed those tags - but have the problem that during the destroy/create cycle the player gets stopped and then restarted. This is okay and I actually do this even manually in onPause() to stop the player when the activity goes in the background.
To keep it running during orientation changes now, I tried making it static (and using the Application Context to create it once). Of course, when I remove the player.stop() in onPause() now, it does what I want - well, until the Activity goes in the background.
So, two questions:
How can I determine if the Activity will be recreated directly after the call to onStop()
Or: How can I keep the MediaPlayer running during that cycle, yet stop it when the App goes in the background?
Have you looked at using the onConfigurationChanged() callback to handle some of this logic?
Regarding your question how you can reliably determine whether your Activity is destroyed due to a configuration change, see my answer here: How to save/restore(update) ref to the dialog during screen rotation?(I need ref in onCreate method of activity.)
With this, the answer to your second question should be easy.
Try running MediaPlayer in different Thread.
You can add to this thread a more complex API to which you can call from onCreate/onStop/on*
If you are using fragments (and you should :P), then calling setRetainInstance(true) inside your fragments onCreate() call will make this problem go completely go away.
Please see the following answer: https://stackoverflow.com/a/31466602/994021
It is a POC I've created for this purpose. Tired of solving this issue over and over again for different projects. I hope it helps.
I really recommend to switch to a third party player like ExoPlayer from google guys: https://github.com/google/ExoPlayer
My app is retaining all of the variable values when it closes and this is effecting how it runs when reopened. Is there any way to reset them all upon app close? or is there a way to clean the app from memory when it is closed so to speak? For the moment I have just been setting all of the important variables "=0" in the last few lines of execution but I know there must be a correct way to doing this.
EDIT:
OK I thought that it would just be easier to reply here instead of individually to everyone.
The app is indeed staying alive in the background, I checked with advanced task killer. How would I get the ap to "Die" by presing the back button? I think this would be the easiest solution given how the app works:
open app > press start button > press stop button > results screen > press back button to exit.
so basically each time the app runs should be an independant execution.
Override the onPause, onResume, and onDestroy methods. onPause should save anything upon pausing, onResume should reload these values when it is resumed, and onDestroy will be called when your app closes. You can clean up stuff in onDestroy. See this link.
You app is probably not closing but remaining in background. Check advanced task manager and see if the app is running or not.
You need to familiarize yourself with the Activity Lifecycle.
You could leverage onResume() to reset your variables; also note onDestory() and onPause().
UPDATE:
Killing the application in its entirety each time the app moves to the background is an anti-pattern. You should really look at your application and follow the aforementioned activity lifecycle pattern and take the needed steps to insure your variables exist as you desire based on state.
I like what #Alex and #Jack said. To add to that, also consider that you can call finish() in your Activity if you want to force it to close up and return to the last Activity. Going along with this, also consider the use of setResult(int) (JavaDoc Here)
You can also set a flag on the Intent when you call the Activity you are questioning about. A flag like FLAG_ACTIVITY_NO_HISTORY could be helpful:
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
List of Intent Flags
Uninitialized variables are bad. Don't do it. ALWAYS manually reset variables before using them for the first time.
the onResume() method will let you reset the variables when the program resumes, but will also do it when you return to the activity unless you add the logic that says you are coming from in the app, not the home page. Maybe onRestart() is what you really need? I'm not positive, but it's possible with onResume.
I'm wondering whether android broadcasts a intent when the screen orientation changes. I am programming a live wallpaper and want to deal with it when the screen orientation changes. I can handle the same with regular apps.
Android system doesn't broadcast an Intent, but restarts the activity by default.
If you want to override this you should override onConfigurationChanged(Configuration) method of your activity. To get this method called you should also specify the changing in android:configChanges attribute in your manifest file.
See what and how to specify here.
I think "android.intent.action.CONFIGURATION_CHANGED" is what you want.
I don't think it does, but you can always check the configuration at a repeating interval to see if it changed. To get the configuration call getResources().getConfiguration() and inspect the orientation.
Well if the app is not in the foreground, this
"onConfigurationChanged(Configuration)"
method does not work (for example when the app displays an overlay in a service)