android – 插件Phonegap电源管理

android – 插件Phonegap电源管理,第1张

概述我试图为 Android安装这个插件.但它没有用:在我的index.html中 function acquire() { cordova.require('cordova/plugin/powermanagement').acquire( function() { alert( 'hooray' ); }, funct 我试图为 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?

我不得不更新PowerManagement插件on github以使其与Cordova 2.8.0一起使用.我还扩展它以获得部分唤醒锁.您可以下载包含更新插件here的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 <[email protected]> */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);    }}

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,'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,["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,["partial"]);    }    var powermanagement = new PowerManagement();    module.exports = powermanagement;});

index.HTML(用于测试)

<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>
总结

以上是内存溢出为你收集整理的android – 插件Phonegap电源管理全部内容,希望文章能够帮你解决android – 插件Phonegap电源管理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1129907.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存