Game Development Reference
In-Depth Information
API, which is a nice wrapper around the Skia library and suitable for modestly complex 2D
graphics. Starting from chapter 7 we'll look into rendering 2D and 3D graphisc with OpenGL.
Before we get to that, we first need to talk about two things: wake locks and going full-screen.
Using Wake Locks
If you leave the tests we wrote so far alone for a few seconds, the screen of your phone will dim.
Only if you touch the screen or hit a button will the screen go back to its full brightness. To keep
our screen awake at all times, we can use a wake lock .
The first thing we need to do is to add a proper <uses-permission> tag in the manifest file with
the name android.permission.WAKE_LOCK . This will allow us to use the WakeLock class.
We can get a WakeLock instance from the PowerManager like this:
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
Like all other system services, we acquire the PowerManager from a Context instance. The
PowerManager.newWakeLock() method takes two arguments: the type of the lock and a tag
we can freely define. There are a couple of different wake lock types; for our purposes, the
PowerManager.FULL_WAKE_LOCK type is the correct one. It will make sure that the screen will stay
on, the CPU will work at full speed, and the keyboard will stay enabled.
To enable the wake lock, we have to call its acquire() method:
wakeLock.acquire();
The phone will be kept awake from this point on, no matter how much time passes without user
interaction. When our application is paused or destroyed, we have to disable or release the wake
lock again:
wakeLock.release();
Usually, we instantiate the WakeLock instance on the Activity.onCreate() method, call
WakeLock.acquire() in the Activity.onResume() method, and call the WakeLock.release()
method in the Activity.onPause() method. This way, we guarantee that our application still
performs well in the case of being paused or resumed. Since there are only four lines of code
to add, we're not going to write a full-fledged example. Instead, we suggest you simply add the
code to the full-screen example of the next section and observe the effects.
Going Full-Screen
Before we dive head first into drawing our first shapes with the Android APIs, let's fix something
else. Up until this point, all of our activities have shown their title bars. The notification bar was
visible as well. We'd like to immerse our players a little bit more by getting rid of those. We can
do that with two simple calls:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
Search WWH ::




Custom Search