Loading...
Logo
Processing Forum
I'm making a simple thing for my new android phone that uses the accelerator to move a ball around the screen. The problem is that after a few seconds, the screen goes black and I need to press my back button to turn it back on. Is there any way to disable sleep mode while my game is running?

While googling around, I found this site here. It says something like "use this flag: WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON"

Anyone know what the heck this means or how I can use it?
I've seen soemthing like this implemented on games and other apps that I've downloaded for my phone, and I've seen soem sortof methods for doing this with java. Has nobody else come across this problem but me?

Apparently someone else has asked a similer question on here. But it seems they didn't get an answer either.

Replies(15)

Android Mode seems to be broken for me right now, so can't check, but I think you have to check a Permission...
If I check this permission, will I also need to include some additional code in my program, or will this have the desired effect automatically? I'll check through the programs that I know to have this feature to see if they have any common permission.
Seems that System Tools is the one I want. There's even a little description underneath it that says "Prevent Phone From Sleeping". Thanks for the hint, and I'll update if this actually works.

Seems like System Tools isn't an option in the Permissions menu in the Processing window. Is there any other way for me to set it?
I tried copy pasting the first part of that at the top of my code and then putting the aquire and release thign inside setup and exit, but I seem to be getting en error along the lines of "Powermanager does not exist". Do I need to include it in my code somewhere or something? I just started this stuff yesterday so I'm kind of new. Any sort of sample code would be very appreciated.
Probably have to put


import android.os.PowerManager

with your other imports at the top of your sketch...

It seems that I'm getting an error whenever I try to import. This black box pops up that says "unexpected token" import". What does that mean I should do?
Really ?!? 

And you've put this at the top of your sketch, before setup(), with your other imports ?
Exactly like that. I AM in android mode. Also when I go to  Sketch -> Import Library it says "Android mode ahs no import libraries"
Am I doing something horrably wrong?
Yes, you are 

To import standard Java or Processing libraries (if they are Android-compatible), you have to use Sketch > Add File, which add's the JAR to the CODE folder.
You don't need to type your imports to your code (though I usually do just to remember what I'm using).

As far as Android methods, you need to type import android... to import the methods/classes, but there's no gui interface to do that. Don't know if that's in the development pipeline or not... 

And can you explain this : 


This black box pops up that says "unexpected token" import". 


What IDE are you using? I don't know that I've ever seen a black box, let alone one popping up, for error messages... 
I'm just using the Processing IDE, and the black box is the stuff jsut over the console at the bottom. I think it just tells you the first error that pops up on the console.
This is the output I'm getting:

processing.app.SketchException: unexpected token: import
    at processing.mode.java.JavaBuild.preprocess(JavaBuild.java:326)
    at processing.mode.android.AndroidBuild.createProject(AndroidBuild.java:135)
    at processing.mode.android.AndroidBuild.build(AndroidBuild.java:80)
    at processing.mode.android.AndroidMode.handleRunEmulator(AndroidMode.java:146)
    at processing.mode.android.AndroidEditor$12.run(AndroidEditor.java:336)

Also, here is a screeenshot.


Also, would switching to Eclipse make things easier?

Edit: I realized after I posted this that the box is actually purple-ish. My bad.
Okay, good news!

I've made a class that lets you start and stop a wakelock.
Here is the code:

import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.content.Context;

public class screenLock {
  PowerManager pm;
  Context context;
  WakeLock wl;

  public screenLock(Context parent) {
    this.context = parent;
    pm =(PowerManager) parent.getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
  }

  public void Start() {
    wl.acquire();
  }

  public void Stop() {
    wl.release();
  }
}

Save that as screenLock.java

Now you can make a new object and call yourobjectname.Start(); to lock the screen and yourobjectname.Stop(); to let it dim.
Don't use wakelocks if you don't have to.

set FLAG_KEEP_SCREEN_ON in onCreate event.

Copy code
  1. import android.os.Bundle;
  2. import android.view.WindowManager;

  3. void onCreate(Bundle bundle) {
  4.   super.onCreate(bundle);
  5.   getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  6. }

Now automatic sleep mode doesn't effect.


In my case, processing activity failed when i pushed power button and then come back to my sketch.

I was using landscape mode and when phone goes to sleep mode it turns it to portrait mode. This seems to cause some kind of fail. And if it didn't force close my app it restarts it.

Solution:

Put this in your AndroidManifest.xml file:
Copy code
  1. <activity android:name="Skecth name" android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden">
and this into your .pde:
Copy code
  1. import android.content.res.Configuration;

  2. public void onConfigurationChanged(Configuration newConfig) {
  3.   super.onConfigurationChanged(newConfig);
  4. }
And all of my force closes and annoying restarts have gone away.

I hope this helps someone.
Nice one xSus - works a charm! Hey If my app is already set in portrait mode, do I still need to update the  AndroidManifest.xml file & add the Configuration updates to the pde?