Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063151
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-16 15:31:59

在Android中,WakeLock可以让进程持续执行,即使手机关屏、进入睡眠模式。。基于Rexsee的WakeLock扩展可以使用JS实现。。

【函数】 void acquire(boolean onAfterRelease)

【说明】 点亮屏幕直到release()被调用。

【参数】 onAfterRelease:在release()被调用后是否继续点亮至默认的屏幕超时。

【示例】

Html代码 复制代码 收藏代码
  1. window.setTimeout('rexseeKeyguard.disable();rexseeWakeLock.acquire(false);alert(\'点亮屏幕!\');',5000);
  2. alert('请按电源键关屏,5秒后自动亮屏。');
window.setTimeout('rexseeKeyguard.disable();rexseeWakeLock.acquire(false);alert(\'点亮屏幕!\');',5000); alert('请按电源键关屏,5秒后自动亮屏。');

【函数】 void release()

【说明】 允许黑屏,如果调用的acquire()函数携带了参数true则仍需等待默认的屏幕超时时间后才会黑屏。

【示例】

Html代码 复制代码 收藏代码
  1. rexseeWakeLock.release();
rexseeWakeLock.release();

rexseeWakeLock.java源码

Java代码 复制代码 收藏代码
  1. /*
  2. * Copyright (C) 2011 The Rexsee Open Source Project
  3. *
  4. * Licensed under the Rexsee License, Version 1.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package rexsee.core.alarm;
  17. import rexsee.core.browser.JavascriptInterface;
  18. import rexsee.core.browser.RexseeBrowser;
  19. import android.content.Context;
  20. import android.os.PowerManager;
  21. public class RexseeWakeLock implements JavascriptInterface {
  22. private static final String INTERFACE_NAME = "WakeLock";
  23. @Override
  24. public String getInterfaceName() {
  25. return mBrowser.application.resources.prefix + INTERFACE_NAME;
  26. }
  27. @Override
  28. public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
  29. return this;
  30. }
  31. @Override
  32. public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
  33. return new RexseeWakeLock(childBrowser);
  34. }
  35. private final Context mContext;
  36. private final RexseeBrowser mBrowser;
  37. private PowerManager.WakeLock mWakeLock = null;
  38. public RexseeWakeLock(RexseeBrowser browser) {
  39. mBrowser = browser;
  40. mContext = browser.getContext();
  41. }
  42. public RexseeWakeLock(Context context) {
  43. mBrowser = null;
  44. mContext = context;
  45. }
  46. //JavaScript interface
  47. public void acquire(boolean onAfterRelease) {
  48. release();
  49. PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
  50. int mode = PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP;
  51. if (onAfterRelease) {
  52. mode = mode | PowerManager.ON_AFTER_RELEASE;
  53. }
  54. mWakeLock = pm.newWakeLock(mode, "");
  55. mWakeLock.acquire();
  56. }
  57. public void release() {
  58. if (mWakeLock != null) {
  59. mWakeLock.release();
  60. mWakeLock = null;
  61. }
  62. }
  63. }
/* * Copyright (C) 2011 The Rexsee Open Source Project * * Licensed under the Rexsee License, Version 1.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package rexsee.core.alarm; import rexsee.core.browser.JavascriptInterface; import rexsee.core.browser.RexseeBrowser; import android.content.Context; import android.os.PowerManager; public class RexseeWakeLock implements JavascriptInterface { private static final String INTERFACE_NAME = "WakeLock"; @Override public String getInterfaceName() { return mBrowser.application.resources.prefix + INTERFACE_NAME; } @Override public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) { return this; } @Override public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) { return new RexseeWakeLock(childBrowser); } private final Context mContext; private final RexseeBrowser mBrowser; private PowerManager.WakeLock mWakeLock = null; public RexseeWakeLock(RexseeBrowser browser) { mBrowser = browser; mContext = browser.getContext(); } public RexseeWakeLock(Context context) { mBrowser = null; mContext = context; } //JavaScript interface public void acquire(boolean onAfterRelease) { release(); PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); int mode = PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; if (onAfterRelease) { mode = mode | PowerManager.ON_AFTER_RELEASE; } mWakeLock = pm.newWakeLock(mode, ""); mWakeLock.acquire(); } public void release() { if (mWakeLock != null) { mWakeLock.release(); mWakeLock = null; } } }仅对Rexsee的源码和函数事件做了整理,相关的demo或源码解析可以在Rexsee社区了解,目前Rexsee已提供了近2000个扩展,覆盖Android原生功能超过90%,且全部开放:
阅读(604) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~