Android 电源管理插件

Android 电源管理插件,android,cordova,phonegap-plugins,Android,Cordova,Phonegap Plugins,我尝试为android安装这个插件。但它不起作用:在我的index.html中 function acquire() { cordova.require('cordova/plugin/powermanagement').acquire( function() { alert( 'hooray' ); }, function() { alert( 'oh no!' ); } );

我尝试为android安装这个插件。但它不起作用:在我的index.html中

function acquire() {
        cordova.require('cordova/plugin/powermanagement').acquire(
                function() { alert( 'hooray' ); },
                function() { alert( 'oh no!' ); }
                );
    };
我没有警报:s

。我把它放在www文件夹里

<script type="text/javascript" charset="utf-8" src="lib/cordova/powermanagement.js">         </script>

然后,在我的AndroidManifest.xml中:

<uses-permission android:name="android.permission.WAKE_LOCK" />

在我的config.xml中

<plugin name="PowerManagement" value="org.apache.cordova.plugin.PowerManagement"/

我看不出你发布的代码有什么问题。您使用的是什么版本的Phonegap

我必须更新电源管理插件,使其与Cordova 2.8.0兼容。我还扩展了它以获得部分唤醒锁。您可以下载包含更新插件的Eclipse项目

以下是用于Cordova 2.8.0的更新代码:

PowerManagement.java

/*
   Copyright 2011-2012 Wolfgang Koller - http://www.gofg.at/

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   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.
*/

/**
 * Cordova (Android) plugin for accessing the power-management functions of the device
 * @author Wolfgang Koller <viras@users.sourceforge.net>
 */
package org.apache.cordova.plugin;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;

/**
 * Plugin class which does the actual handling
 */
public class PowerManagement extends CordovaPlugin {
    // As we only allow one wake-lock, we keep a reference to it here
    private PowerManager.WakeLock wakeLock = null;
    private PowerManager powerManager = null;

    /**
     * Fetch a reference to the power-service when the plugin is initialized
     */
    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);

        this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);
    }

    @Override
    public boolean execute(String action, JSONArray args,
            CallbackContext callbackContext) throws JSONException {

        Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() );
        Log.d("PowerManagementPlugin", "Action is " + action );

        try {
            if( action.equals("acquire") ) {                
                String type = args.optString(0);
                if(type.equals("dim") ) {
                    Log.d("PowerManagementPlugin", "Only dim lock" );
                    this.acquire( PowerManager.SCREEN_DIM_WAKE_LOCK );
                }
                else if(type.equals("partial") ) {
                    Log.d("PowerManagementPlugin", "Only partial lock" );
                    this.acquire( PowerManager.PARTIAL_WAKE_LOCK );
                }
                else {
                    Log.d("PowerManagementPlugin", "Full wakelock" );
                    this.acquire( PowerManager.FULL_WAKE_LOCK );
                }
            }
            else if( action.equals("release") ) {
                this.release();
            }
        }
        catch( Exception e ) {
            return false;
        }

        callbackContext.success();
        return true;
    }

    /**
     * Acquire a wake-lock
     * @param p_flags Type of wake-lock to acquire
     */
    private void acquire( int p_flags ) {

        if (this.wakeLock == null) {
            this.wakeLock = this.powerManager.newWakeLock(p_flags, "PowerManagementPlugin");
            try {
                this.wakeLock.acquire();
            }
            catch( Exception e ) {
                this.wakeLock = null;
            }
        }
    }

    /**
     * Release an active wake-lock
     */
    private void release() {

        if( this.wakeLock != null ) {
            this.wakeLock.release();
            this.wakeLock = null;

        }
    }

    /**
     * Make sure any wakelock is released if the app goes into pause
     */
    @Override
    public void onPause(boolean multitasking) {
        if( this.wakeLock != null ) this.wakeLock.release();

        super.onPause(multitasking);
    }

    /**
     * Make sure any wakelock is acquired again once we resume
     */
    @Override
    public void onResume(boolean multitasking) {
        if( this.wakeLock != null ) this.wakeLock.acquire();

        super.onResume(multitasking);
    }
}
/*
版权所有2011-2012 Wolfgang Koller-http://www.gofg.at/
根据Apache许可证2.0版(以下简称“许可证”)获得许可;
除非遵守许可证,否则不得使用此文件。
您可以通过以下方式获得许可证副本:
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,软件
根据许可证进行的分发是按“原样”进行分发的,
无任何明示或暗示的保证或条件。
请参阅许可证以了解管理权限和权限的特定语言
许可证下的限制。
*/
/**
*Cordova(Android)插件,用于访问设备的电源管理功能
*@作者沃尔夫冈·科勒
*/
包org.apache.cordova.plugin;
导入org.json.JSONArray;
导入org.json.JSONException;
导入android.content.Context;
导入android.os.PowerManager;
导入android.util.Log;
导入org.apache.cordova.CordovaWebView;
导入org.apache.cordova.api.CallbackContext;
导入org.apache.cordova.api.cordova接口;
导入org.apache.cordova.api.CordovaPlugin;
/**
*执行实际处理的插件类
*/
公共类电源管理扩展了CordovaPlugin{
//因为我们只允许一个唤醒锁,所以我们在这里保留对它的引用
private PowerManager.WakeLock WakeLock=null;
专用PowerManager PowerManager=null;
/**
*在插件初始化时获取对电源服务的引用
*/
@凌驾
公共无效初始化(cordova接口cordova、CordovaWebView webView){
super.initialize(cordova、webView);
this.powerManager=(powerManager)cordova.getActivity().getSystemService(Context.POWER\u服务);
}
@凌驾
公共布尔执行(字符串操作、JSONArray参数、,
CallbackContext(CallbackContext)抛出JSONException{
Log.d(“PowerManagementPlugin”,“插件执行调用-”+this.toString());
Log.d(“PowerManagementPlugin”,“操作是”+操作);
试一试{
if(action.equals(“acquire”){
字符串类型=args.optString(0);
if(类型等于(“dim”)){
Log.d(“PowerManagementPlugin”,“仅暗锁”);
获取(PowerManager.SCREEN\u DIM\u WAKE\u LOCK);
}
else if(类型等于(“部分”)){
Log.d(“PowerManagementPlugin”,“仅部分锁定”);
获取(PowerManager.PARTIAL\u WAKE\u LOCK);
}
否则{
Log.d(“PowerManagementPlugin”,“完全唤醒锁”);
获取(PowerManager.FULL\u WAKE\u LOCK);
}
}
else if(action.equals(“release”)){
这个。释放();
}
}
捕获(例外e){
返回false;
}
callbackContext.success();
返回true;
}
/**
*获得尾流锁
*@param p_标志要获取的唤醒锁类型
*/
私有无效获取(int p_标志){
if(this.wakeLock==null){
this.wakeLock=this.powerManager.newWakeLock(p_标志,“PowerManagementPlugin”);
试一试{
this.wakeLock.acquire();
}
捕获(例外e){
this.wakeLock=null;
}
}
}
/**
*释放活动尾流锁
*/
私人无效释放(){
如果(this.wakeLock!=null){
这个.wakeLock.release();
this.wakeLock=null;
}
}
/**
*如果应用程序进入暂停状态,请确保任何wakelock都已释放
*/
@凌驾
public void onPause(布尔多任务){
如果(this.wakeLock!=null)this.wakeLock.release();
super.onPause(多任务处理);
}
/**
*确保恢复后再次获取任何wakelock
*/
@凌驾
公共void onResume(布尔多任务){
如果(this.wakeLock!=null)this.wakeLock.acquire();
super.onResume(多任务处理);
}
}
powermanagement.js

/*
 * Copyright (C) 2011-2012 Wolfgang Koller
 * 
 * This file is part of GOFG Sports Computer - http://www.gofg.at/.
 * 
 * GOFG Sports Computer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GOFG Sports Computer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GOFG Sports Computer.  If not, see <http://www.gnu.org/licenses/>.
 */
cordova.define("cordova/plugin/powermanagement", function(require, exports, module) {
    var exec = require('cordova/exec');

    var PowerManagement = function() {};

    /**
     * Acquire a full wake-lock (keep device awake)
     * 
     * @param successCallback function to be called when the wake-lock was acquired successfully
     * @param errorCallback function to be called when there was a problem with acquiring the wake-lock
     */
    PowerManagement.prototype.acquire = function(successCallback,failureCallback) {
        cordova.exec(successCallback, failureCallback, 'PowerManagement', 'acquire', []);
    }

    /**
     * Release the wake-lock
     * 
     * @param successCallback function to be called when the wake-lock was released successfully
     * @param errorCallback function to be called when there was a problem while releasing the wake-lock
     */
    PowerManagement.prototype.release = function(successCallback,failureCallback) {
        cordova.exec(successCallback, failureCallback, 'PowerManagement', 'release', []);
    }

    /**
     * Acquire a partial wake-lock, allowing the device to dim the screen
     *
     * @param successCallback function to be called when the wake-lock was acquired successfully
     * @param errorCallback function to be called when there was a problem with acquiring the wake-lock
     */
    PowerManagement.prototype.dim = function(successCallback,failureCallback) {
        cordova.exec(successCallback, failureCallback, 'PowerManagement', 'acquire', ["dim"]);
    }

    /**
     * Acquire a partial wake-lock, allowing the device to turn off the screen but keep the CPU active
     *
     * @param successCallback function to be called when the wake-lock was acquired successfully
     * @param errorCallback function to be called when there was a problem with acquiring the wake-lock
     */
    PowerManagement.prototype.partial = function(successCallback,failureCallback) {
        cordova.exec(successCallback, failureCallback, 'PowerManagement', 'acquire', ["partial"]);
    }


    var powermanagement = new PowerManagement();
    module.exports = powermanagement;
});
/*
*版权所有(C)2011-2012 Wolfgang Koller
* 
*此文件是GOFG运动计算机的一部分-http://www.gofg.at/.
* 
*GOFG运动电脑是免费软件:您可以重新发布和/或修改它
*它是根据GNU通用公共许可证的条款发布的
*自由软件基金会,或者许可证的第3版,或者
*(由您选择)任何更高版本。
* 
*GOFG运动计算机是分布式的,希望它会有用,
*但无任何保证;甚至没有任何关于
*适销性或适合某一特定目的。见
*有关更多详细信息,请参阅GNU通用公共许可证。
* 
*您应该已经收到GNU通用公共许可证的副本
*还有GOFG运动电脑。如果没有,请参阅。
*/
定义(“cordova/plugin/powermanagement”),函数(需求、导出、模块){
var exec=require('cordova/exec');
var PowerManagement=function(){};
/**
*获取完全唤醒锁定(保持设备唤醒)
* 
*@param successCallback函数,在成功获取唤醒锁时调用
<html>
    <head>
        <script type="text/javascript" charset="utf-8" src="cordova-2.8.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="powermanagement.js"></script>

        <script type="text/javascript">
        function deviceready() {
            alert( 'cordova ready!' );
        }

        function acquire() {
            cordova.require('cordova/plugin/powermanagement').acquire(
                    function() { alert( 'successfully acquired full wake lock' ); },
                    function() { alert( 'error acquiring full wake lock' ); }
                    );
        };

        function release() {
            cordova.require('cordova/plugin/powermanagement').release(
                    function() { alert( 'successfully released wake lock' ); },
                    function() { alert( 'error releasing wake lock' ); }
                    );
        }

        function dim() {
            cordova.require('cordova/plugin/powermanagement').dim(
                    function() { alert( 'successfully acquired dim wake lock!' ); },
                    function() { alert( 'error acquiring dim wake lock' ); }
                    );
        }

        function partial() {
            cordova.require('cordova/plugin/powermanagement').partial(
                    function() { alert( 'successfully acquired partial wake lock!' ); },
                    function() { alert( 'error acquiring partial wake lock' ); }
                    );
        }

        document.addEventListener("deviceready", deviceready, true);
        </script>
    </head>
    <body>
    <button type="button" onclick="acquire();">acquire</button>
    <br />
    <button type="button" onclick="release();">release</button>
    <br />
    <button type="button" onclick="dim();">dim</button>
    <br />
    <button type="button" onclick="partial();">partial</button>
    </body>
</html>