如何在需要时获得cordova 2.9的电池电量?

时间:2014-01-31 11:13:40

标签: cordova

window.addEventListener("batterystatus", onBatteryStatus, false);
        function onBatteryStatus(info) {
            // Handle the online event
            console.log("Level: " + info.level + " isPlugged: " + info.isPlugged); 
        }

我希望获得电池电量差距2.9.they提供这个以获得电池电量和状态

但它只会激发电池状态的变化,但是当我们需要它时它无法触发。有没有办法在cordova中获得电池电量?

3 个答案:

答案 0 :(得分:3)

在需要时无法获得电池电量。该方法仅在电池状态改变或电池电量变化时触发。我已经编写了一个插件,可以随时随地获取电池电量。

在源文件夹

中创建这些java文件

在源文件夹中创建Bat_lev插件

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




}

以下java类为我们提供电池电量

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

编写此javascript代码以获取batterylevel

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

    }

并确保将此行添加到config.xml

<feature name="Bat_lev" >
        <param
            name="android-package"
            value="org.apache.cordova.example.Bat_lev" />
    </feature>

答案 1 :(得分:2)

你无法在phonegap中解决这个问题。但是你可以编写插件来完成它。如果你使用android,下面的代码可能会有所帮助,你可以调整插件类。

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 + "%");
    }
};

}

希望有所帮助

答案 2 :(得分:0)

当尝试在Cordova 2.3.0版上访问电池电量时,我遇到了类似的问题。我尝试制作插件,但未成功。然后我想,为什么不使用普通的老式js

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

});