Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 插件Phonegap电源管理大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图为 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"/

我的代码有什么问题?

谢谢.@H_262_29@奥莱丽亚

解决方法

我发布的代码没有任何问题.您使用的是什么版本的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 <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,'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电源管理所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。