本文介绍了如何获得电池电量在cordova 2.9当它需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想在cordova phone-gap 2.9.they提供这个获得电池电量和状态

i want get battery level in cordova phone-gap 2.9.they provided this to get battery level and status

但它只是触发电池状态更改,但它不能触发,当我们需要它。有办法获得电池电量cordova? / p>

but it only fire battery status change but it can't fire when we need it.is there a way to get battery level in cordova?

推荐答案

没有办法在需要时获取电池电量。只有当电池状态变化或电池电量变化时,

There is no way to get battery level when it need.that method fires only when battery status change or battery level change.here i have written a plugin for get battery level any where any time.

在源文件夹中创建这些java文件

create these java files in source folder

在源文件夹中创建Bat_lev插件

create Bat_lev plugin in source folder

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类为我们提供电池电量

following java class provide battery level to us

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代码以获取电子邮件

write this javascript code to get 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

and make sure you add this line to config.xml

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

这篇关于如何获得电池电量在cordova 2.9当它需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 10:32