需要时,如何在cordova 2.9中获得电池电量?
我想在cordova phone gap 2.9中获取电池电量。他们提供此信息是为了获取电池电量和状态需要时,如何在cordova 2.9中获得电池电量?,cordova,Cordova,我想在cordova phone gap 2.9中获取电池电量。他们提供此信息是为了获取电池电量和状态 但它只能触发电池状态更改,但在我们需要时无法触发。有没有办法在cordova中获取电池电量?你不能在phonegap中解决它。但是你可以编写插件来完成它。如果你使用安卓,下面的代码可能会有所帮助,你可以针对插件类对其进行调整 window.addEventListener("batterystatus", onBatteryStatus, false); function on
但它只能触发电池状态更改,但在我们需要时无法触发。有没有办法在cordova中获取电池电量?你不能在phonegap中解决它。但是你可以编写插件来完成它。如果你使用安卓,下面的代码可能会有所帮助,你可以针对插件类对其进行调整
window.addEventListener("batterystatus", onBatteryStatus, false);
function onBatteryStatus(info) {
// Handle the online event
console.log("Level: " + info.level + " isPlugged: " + info.isPlugged);
}
}
希望它有帮助在需要时没有办法获取电池电量。该方法仅在电池状态更改或电池电量更改时启动。在这里,我编写了一个插件,用于随时随地获取电池电量 在源文件夹中创建这些java文件 在源文件夹中创建Bat_lev插件
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class BatteryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(batteryStatusReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
Log.d("BatteryActivity", "Battery level: " + level + "%");
}
};
以下java类为我们提供电池电量
package org.apache.cordova.example;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class Bat_lev extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("echo")) {
String message = args.getString(0);
this.echo(message, callbackContext);
return true;
}
return false;
}
private void echo(String message, CallbackContext callbackContext) {
PowerConnectionReceiver p1=new PowerConnectionReceiver();
Context context=cordova.getActivity();
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
p1.onReceive(cordova.getActivity(), batteryStatus);
callbackContext.success(""+ p1.BAT_LEVEL);
}
}
编写此javascript代码以获得batterylevel
package org.apache.cordova.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
public class PowerConnectionReceiver extends BroadcastReceiver {
static int BAT_LEVEL;
@Override
public void onReceive(Context context, Intent intent) {
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale;
}
BAT_LEVEL = level;
}
}
并确保将这一行添加到config.xml
function bat_level() {
window.echo = function(str, callback) {
cordova.exec(callback, function(err) {
callback('Nothing to echo.');
}, "Bat_lev", "echo", [str]);
};
window.echo("echome", function(echoValue) {
console.log(echoValue);
localStorage.bat_level=echoValue;//u can use this value anywhere u want
});
}
在尝试访问cordova 2.3.0版的电池电量时,我遇到了类似的问题。我尝试制作一个插件,但没有成功。然后我想,为什么不使用普通的香草js呢
<feature name="Bat_lev" >
<param
name="android-package"
value="org.apache.cordova.example.Bat_lev" />
</feature>
您好,是否有可能使用Phonegap构建?我也有同样的问题,因为我想在需要的时候获得batterystatus(IOS和Android)。谢天谢地,这没用。。你能分享你的文件夹结构吗?你把这些文件和它们的名字放在哪里了?嘿,我几年前就这么做了。现在我没有源代码了
navigator.getBattery().then(function(battery) {
var level = battery.level;
alert(level * 100 + "%");
});
navigator.getBattery().then(function(battery) {
var level = battery.level;
//do what you want with battery level
});