As in Unity3D turn off the screen without entering sleep mode, so that the application continues to work?
I don't believe it's possible to have Unity run in the background.
I don't know if this is still common practice, but in the past, many people simply draw a fully black screen to conserve power.
See: https://forum.unity.com/threads/application-runinbackground-is-not-working-on-android.117723/
Have you looked at - https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html
OnApplicationFocus is called when the application loses or gains focus. Alt-tabbing or Cmd-tabbing can take focus away from the Unity application to another desktop application. This causes the GameObjects to receive an OnApplicationFocus call with the argument set to false. When the user switches back to the Unity application, the GameObjects receive an OnApplicationFocus call with the argument set to true.
You could implement this with the gameobjects you want to continue behaving as you like.
Related
I am building a simple game where 2 people can duel against each other. I am using GameSparks as a back end. My problem is, that whenever player #1 casts a spell that is channeling, player #2 can see the channeling only when his screen is turned on and in focus.
Whenever player's #2 screen turns off, the app is not active anymore so the spell is not being casted at him. When player #2 turns on the screen, the channeling starts, but player #1 was already done channeling a while ago.
I assume that is related to the way Android system works. It puts apps that are not in focus on pause.
My problem now is, how can I make sure that player #2 gets a notification whenever he is hit by the spell without a need to use any third party service to keep the app alive (to avoid huge battery consumption) and also without a need to use push notifications (as from what I've heard, if there is a high load, the push doesn't happen right away, since there is a queue)
Application.runInBackground is only for Webplayer (and PC, not sure about the last one though).
Unity runs as Activity in Android, hence it gets paused when it's loses it's focus. That's by (Android) Design and works as intended.
Topic link
So I am making an android game, and so far it's working fairly well. I was using System.exit(0) to kill it, but I know that is an improper way to do it. So I used finish() instead, which does close the program. The problem is, that when I reopen it, the screen is blank. It seems that for some reason, the glSurfaceView isn't being released, and continues to run, and that upon reopening the game, it loses focus of it? The sounds and everything play, and my Logcats display information, so the game is in fact running, but just not the screen. Now before people start jumping on the (just let android take care of closing your app bandwagon) I have my reasons for doing so. For one, telling me that android is efficient enough to do this is a lie, otherwise we would not have the need to restart the devices every other day. Nothing more bothersome that a lag driven phone that can barely even handle an incoming call because it has so much shit on it running. So yea, I would like to be able to close the app.
So yea, how does one correctly to the glSurfaceView that it is time to close itself down? or to tell the Activities class that it needs to release it's handle to the glSurfaceView since closing the app sure doesn't do that.
I am developing an Android app. I have already crossed more than 20 Activities. So I am bit concerned about it. I mean if there are more Activities in an Android app, does it affect the performance of App like Speed, Memory or any other issue?
Though it is not a standard question but still I feel its something which might help others too
Yes Suraj more activities will affect performance
An activity is the equivalent of a Frame/Window in GUI toolkits. It takes up the entire drawable area of the screen (minus the status and title bars on top). Activities are meant to display the UI and get input from the user
An Activity (calling-Activity) can spawn another Activity (sub-Activity) in 2 ways:
Fire and forget – create an event (Intent) and fire it
Async callback – create an event (Intent), fire it, and wait for it’s response in a callback method (of the calling-Activity).
So the effect of activities will depend on performance of your device, its processor and memory etc.
Even if any activity will remain in stack and not finish then it affects device performance.
Even you have to take a look at security measures.
I've seen two other similar questions, but no answers that work.
Control screen brightness in android using " Background Service"
Android Development: Changing Screen Brightness in Service
I have a background service that is trying to do some custom power management, and needs to be able to independently control the screen locking (either forcing it to lock immediately or keep it from locking) and also the display state (force it to turn off, on, or dim). I'm having a hard time finding such controls. We're developing for a device, so one option is to modify Android source code, but we're trying very hard to avoid that.
I can use PowerManager.WakeLocks to passively suggest screen brightness (and so can anyone else, so it's not guaranteed). This is okay for preventing screen lock and keeping the display on.
I did see a PowerManager.goToSleep() call for forcing the lock screen - that works. But it doesn't force the display to turn off (or something else is preventing that).
Are there any Android OS API's for this? I can't find them in the online dev guide.
To modify the brightness, I'm using
import android.provider.Settings.system;
...
ContentResolver contentResolver = Application.getContext().getContentResolver();
...
System.putInt(contentResolver, System.SCREEN_BRIGHTNESS, value);
I opened my application after I am not doing any kind of operation. I leave the mobile 5 min. After 5 min when I touch the my application it is showing black screen. Is there any way to avoid this idle state?
This is easy to do. Use: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); in your onCreate method.
There is another way, called a WakeLock, but you really shouldn't use that unless you know what you're doing. See this post from one of the Android designers for more information about WakeLocks and why you shouldn't use them unless you're careful.