我看不出你发布的代码有任何问题。你使用的Phonegap版本是什么?
我不得不更新PowerManagement插件
在github上,以使其与Cordova 2.8.0配合使用。我还将其扩展为能够获取部分唤醒锁定。你可以在
这里下载包含更新插件的Eclipse项目。
以下是适用于Cordova 2.8.0的更新代码:
PowerManagement.java
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;
public class PowerManagement extends CordovaPlugin {
private PowerManager.WakeLock wakeLock = null;
private PowerManager powerManager = null;
@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;
}
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;
}
}
}
private void release() {
if( this.wakeLock != null ) {
this.wakeLock.release();
this.wakeLock = null;
}
}
@Override
public void onPause(boolean multitasking) {
if( this.wakeLock != null ) this.wakeLock.release();
super.onPause(multitasking);
}
@Override
public void onResume(boolean multitasking) {
if( this.wakeLock != null ) this.wakeLock.acquire();
super.onResume(multitasking);
}
}
powermanagement.js
cordova.define("cordova/plugin/powermanagement", function(require, exports, module) {
var exec = require('cordova/exec');
var PowerManagement = function() {};
PowerManagement.prototype.acquire = function(successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'acquire', []);
}
PowerManagement.prototype.release = function(successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'release', []);
}
PowerManagement.prototype.dim = function(successCallback,failureCallback) {
cordova.exec(successCallback, failureCallback, 'PowerManagement', 'acquire', ["dim"]);
}
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>