在Android上获得电池电量

时间:2014-04-25 10:49:56

标签: android

我正在使用此代码来获取电池电量

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
//Getting Battery
getBatteryPercentage();
Log.e("level",""+getBatteryPercentage());
return rootView;
 }
public  int getBatteryPercentage() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver(
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (currentLevel > 0 && scale > 0) {
      level = (currentLevel * 100) / scale;
 }
 }
 }; 
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
getActivity().registerReceiver(batteryLevelReceiver, batteryLevelFilter);
return level;
   }

我没有收到错误,但是level的值总是等于0.

4 个答案:

答案 0 :(得分:1)

可能是由于隐式类型转换

level = (int)(((double)currentLevel * 100) / (double)scale);

同时确保currentLevel和scale都有值。

Log.e("currentLevel ",""+currentLevel);
Log.e("scale",""+scale);

答案 1 :(得分:1)

试试这个..

    @Override
    public void onCreate() {
    BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryReceiver, filter);
}

并且还添加  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>中的mainfiest ...

答案 2 :(得分:0)

只需在代码中运行时执行adb命令

adb shell cat /sys/class/power_supply/battery/capacity

答案 3 :(得分:0)

这是一个获取电池状态的工作程序

void getbatterylevel() {
        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;
        StringBuilder sbstdOut = new StringBuilder();
        StringBuilder sbstdErr = new StringBuilder();



        try { // Run Script

            proc = runtime.exec("cat /sys/class/power_supply/battery/capacity");
            osw = new OutputStreamWriter(proc.getOutputStream());

                    osw.flush();
            osw.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();                    
                }
            }
        }
        try {
            if (proc != null)
                proc.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sbstdOut.append(ReadBufferedReader(new InputStreamReader(proc
                .getInputStream())));
        sbstdErr.append(ReadBufferedReader(new InputStreamReader(proc
                .getErrorStream())));
        if (proc.exitValue() != 0) {
                    }
            }
private Object ReadBufferedReader(InputStreamReader inputStreamReader) {
    // TODO Auto-generated method stub

    BufferedReader  br = new BufferedReader(inputStreamReader);

    int value=0;

    // reads to the end of the stream 
    try {
        while((value = br.read()) != -1)
        {
           // converts int to character
           char c = (char)value;

           // prints character
           System.out.println(c);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

 }