【问题标题】:Plugin Phonegap Power Management插件 Phonegap 电源管理
【发布时间】:2013-06-22 12:38:58
【问题描述】:

我尝试为 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"/

我的代码有什么问题?

谢谢。 奥蕾莉

【问题讨论】:

    标签: android cordova phonegap-plugins


    【解决方案1】:

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

    我必须更新 PowerManagement 插件 on github 才能使其与 Cordova 2.8.0 一起使用。我还将它扩展为能够获取部分唤醒锁。您可以下载我的 Eclipse 项目,其中包含更新的插件here

    这是用于 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);
        }
    }
    

    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;
    });
    

    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>
    

    【讨论】:

    • 感谢您的回答。我的科尔多瓦版本是 2.7.0。您的 elipse 项目的链接不起作用:s。我可以有一个好的;)。因此,如果不是科尔多瓦的版本,则意味着我的代码中有一个问题:s。我会试试你的代码。
    • 该链接对我仍然有效 - 再试一次:http://ge.tt/5ks6w1k/v/0
    • 我试了两次。但现在我明白了;)非常感谢。我会试试你的文件。
    • 好的,我拿走了你的代码,在 Eclipse 中我有一个警告: 描述 资源路径位置类型 不推荐使用 PowerManager.FULL_WAKE_LOCK 字段 PowerManagement.java /projet/src/org/apache/cordova/plugin 第 73 行Java 问题这正常吗?此外,您的代码中的警报不起作用
    • 这是我的代码,console.log 不知道我的功能是(GPS): if( (sexeSeek !== '') && (seek !== '') && (orientation ! == '') && (age !== '') && (slider1 === 'on')) { watchInterval = true;观察位置(); function acquire() { cordova.require('cordova/plugin/powermanagement').acquire( function() { console.log( '成功获得完全唤醒锁' ); }, function() { console.log('错误获取完全唤醒锁' ); } ); };
    猜你喜欢
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多