[PhoneGap] Android: Disable automatic screen locking?

1,741 views
Skip to first unread message

mobweb

unread,
May 13, 2010, 12:06:37 PM5/13/10
to phonegap
Hey,

My application has an important notification feature that vibrates the
phone. Now unfortunately this can not be triggered when the screen has
been locked for more than about a minute. Is there a way to disable
the automatic screen locking when my application is running?

Regards,

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en

For more info on PhoneGap or to download the code go to www.phonegap.com

mobweb

unread,
May 13, 2010, 12:22:27 PM5/13/10
to phonegap
I found this via google:

http://www.androidsnippets.org/snippets/43/

I know how to change androidmanifest.xml, but I'm not sure where I
have to past the top part.

Like I said this is essential for my application to work. :/

On 13 Mai, 18:06, mobweb <i...@mobweb.ch> wrote:
> Hey,
>
> My application has an important notification feature that vibrates the
> phone. Now unfortunately this can not be triggered when the screen has
> been locked for more than about a minute. Is there a way to disable
> the automatic screen locking when my application is running?
>
> Regards,
>
> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en

filmaj

unread,
May 13, 2010, 2:53:05 PM5/13/10
to phonegap
Just pop that Java code into DroidGap.java, in the onCreate function
somewhere. That will disable the key guard for the entire application
lifecycle, by the way.

mobweb

unread,
May 13, 2010, 6:00:51 PM5/13/10
to phonegap
Thanks Fil, it seems to have worked, but it doesn't do what I want.
The only thing that is different now is that after the screen has been
locked (manually or automaticaly) there's no "swipe-to-unlock" thingy
when I unlock, but the view goes right back to my application after I
press the (hardware) unlock button.
What I want however is for the app never to go into the auto screen
lock so the phone stays active! :S
I think that should be possible, because games that don't require any
touching or button pressing, don't lock the screen, either.

mobweb

unread,
May 13, 2010, 6:15:52 PM5/13/10
to phonegap
It's me again, pardon the spam, but I think I found the solution, yet
again don't know how to implement it:

The solution to my problem seems to be the "PARTIAL_WAKE_LOCK" flag
as described here:
http://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK

I also found a snippet on how to implement it:
http://www.androidsnippets.org/snippets/53/ (it should be ok to
replace FULL_WAKE_LOCK with PARTIAL_WAKE_LOCK I guess?)

However, when I paste the snippet linked above into the onCreate
function in DroidGap.java, Eclipse presents me some errors:
For the top part, with the "import ..", the error is:
"android.app.Activity can not be resolved" for each one of the imports
as well as "syntax error on token 'import', assert expected." There
are other errors but I assume that's because the import doesn't work?

Hate to bother you with this but I have no experience whatsoever with
Java/Eclipse (that's why I'm using PhoneGap ;))

Thanks

Jeremy Wadsack

unread,
May 14, 2010, 11:40:55 AM5/14/10
to mobweb, phonegap
It sounds like you have a syntax error that's preventing javac/eclipse from parsing the code. Double check your semicolons, parentheses, braces, etc.

Or... you're only pasting the part from the OnCreate in the snippet into your OnCreate, right? You're not putting the whole snippet into OnCreate, ar you? That would definitely fail. 

Your OnCreate method should look like this when you are done:

  1. @Override  
  2.     protected void onCreate(Bundle savedInstanceState) {  
  3.         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);  
  4.         wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotDimScreen");  
  5.     }  
 

--
Jeremy Wadsack

mobweb

unread,
May 14, 2010, 2:46:55 PM5/14/10
to phonegap
Hey Jeremy,

Thanks a lot for your suggestion. However I'm still a bit unsure:

- In DroidGap.java, there's already an onCreate method, but that one
is "public void" and what you posted uses "protected void". Can I just
add:
----
PowerManager pm = (PowerManager)
getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotDimScreen");
---
To this method? Because if I do, Eclipse still throws an error: "wl
can not be resolved"

- Also, what about the import stuff? Can I just copy that into the top
part of Droidgap.java

If it's possible, could you pastebin your working Droidgap.java?
Because the only thing I need to change in that file is to add that
specific function to enable the PARTIAL_WAKE_LOCK during the lifecycle
of my application. I think that would probably be the easiest way. :)

Thanks,

Louis

On 14 Mai, 17:40, Jeremy Wadsack <jeremy.wads...@gmail.com> wrote:
> It sounds like you have a syntax error that's preventing javac/eclipse from
> parsing the code. Double check your semicolons, parentheses, braces, etc.
>
> Or... you're only pasting the part from the OnCreate in the snippet into
> your OnCreate, right? You're not putting the whole snippet into OnCreate, ar
> you? That would definitely fail.
>
> Your OnCreate method should look like this when you are done:
>
>    1. @Override
>    2.     protected void onCreate(Bundle savedInstanceState) {
>    3.
>            PowerManager pm = (PowerManager)
> getSystemService(Context.POWER_SERVICE);
>    4.         wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
>    "DoNotDimScreen");
>    5.     }
>
> --
> Jeremy Wadsack
>
>
>
> On Thu, May 13, 2010 at 3:15 PM, mobweb <i...@mobweb.ch> wrote:
> > It's me again, pardon the spam, but I think I found the solution, yet
> > again don't know how to implement it:
>
> > The solution to my problem seems to be the  "PARTIAL_WAKE_LOCK" flag
> > as described here:
>
> >http://developer.android.com/reference/android/os/PowerManager.html#P...
>
> > I also found a snippet on how to implement it:
> >http://www.androidsnippets.org/snippets/53/(it should be ok to
> > > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > > > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "phonegap" group.
> > > > > To post to this group, send email to phon...@googlegroups.com
> > > > > To unsubscribe from this group, send email to
> > > > > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > > > Groups "phonegap" group.
> > > > To post to this group, send email to phon...@googlegroups.com
> > > > To unsubscribe from this group, send email to
> > > > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > > For more info on PhoneGap or to download the code go
> > towww.phonegap.com
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "phonegap" group.
> > > To post to this group, send email to phon...@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > > For more options, visit this group athttp://
> > groups.google.com/group/phonegap?hl=en?hl=en
>
> > > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "phonegap" group.
> > To post to this group, send email to phon...@googlegroups.com
> > To unsubscribe from this group, send email to
> > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>

Jeremy Wadsack

unread,
May 14, 2010, 4:28:27 PM5/14/10
to mobweb, phonegap
Don't have a working file, so can't paste anything. I'm just trying to go understand what you have.

You should be able to just add those two lines in your existing onCreate method. Mine are public on other projects, not sure why that snippet is protected.

The easiest way to get all the imports correct in Eclipse is just to press Shit-Cmd-O (or Shift-Ctrl-O on Windows) -- Eclipse will look everything up and put in the proper import statements and clean it all for you.

 
--
Jeremy Wadsack

mobweb

unread,
May 14, 2010, 6:10:21 PM5/14/10
to phonegap
Hey man, thanks for going trough the trouble... :)

Here's what I did, step by step:

1. Pasted the code you posted above into a brand new DroidGap.java
(http://github.com/phonegap/phonegap-android/blob/master/framework/src/
com/phonegap/DroidGap.java).

Here's what my file looks like now:

...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
/* PASTED CODE STARTS HERE */
PowerManager pm = (PowerManager)
getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
/* PASTED CODE ENDS HERE */
...

2. Pressed Shift-Ctrl-O, which seemed to add a few lines to the import
stuff and solve 1 error.

3. Now Eclipse showed me another error on this line: "wl =
pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotDimScreen");",
I right-clicked the error symbol and selected "Create local variable
wl" which changed "wl = ..." to "WakeLock wl = ..." No more errors
displayed now!

4. Then I added "<uses-permission
android:name="android.permission.WAKE_LOCK" />" to AndroidManifest.xml
right below the other permissions.

5. Ran the project on my Nexus one... But after about a minute the
screen was locked automaticaly :(

Here's my DroidGap.java & AndroidManifest.xml:

http://pastebin.org/237150
http://pastebin.org/237151

Jeremy Wadsack

unread,
May 14, 2010, 8:35:45 PM5/14/10
to mobweb, phonegap
You aren't getting any wake lock because you haven't called acquire() on your wake lock object. See the reference: http://developer.android.com/reference/android/os/PowerManager.html#newWakeLock(int,%20java.lang.String)

You should use a private field, not a local variable for the WakeLock and add the call to release onPause and acquire onResume, per the original snippet. That will start the wake lock. The onPause call will protect your from keeping the phone unlocked even when your activity/app in not visible. 



--
Jeremy Wadsack

mobweb

unread,
May 15, 2010, 5:07:09 AM5/15/10
to phonegap
Now it finally worked, thanks Jeremy! :)

For anyone who finds this thread, here's what I changed to a brand new
android phonegap download:

- http://pastebin.com/q1Lk2Ews (src/com/phonegap/DroidGap.java)
- http://pastebin.com/REnPgz7e (AndroidManifest.xml)

The flag that I used was PARTIAL_WAKE_LOCK, which allows for the
screen to be turned of automatically, but the CPU always stays active.
Other flags can be found here: http://developer.android.com/reference/android/os/PowerManager.html

On 15 Mai, 02:35, Jeremy Wadsack <jeremy.wads...@gmail.com> wrote:
> You aren't getting any wake lock because you haven't called acquire() on
> your wake lock object. See the reference:http://developer.android.com/reference/android/os/PowerManager.html#n...)
> > phonegap+u...@googlegroups.com<phonegap%2Bunsu...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/phonegap?hl=en?hl=en
>
> > For more info on PhoneGap or to download the code go towww.phonegap.com
>
> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en
Reply all
Reply to author
Forward
0 new messages