1.通过读取电流节点的方式,获取电流。
电流节点:”/sys/class/power_supply/battery/BatteryAverageCurrent”
2.代码:
..............................................................
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
..............................................................
public class Xxxxx extends Activity{
.....................
private static final String CHARGER_CURRENT_NOW =
"/sys/class/power_supply/battery/BatteryAverageCurrent";
..............................................................
private TextView mCurrent;
..............................................................
public void onResume(){
..............................................................
mCurrent = (TextView)findViewById(R.id.current);
..............................................................
try {
mCurrent.setText(readCurrentFile(new File(CHARGER_CURRENT_NOW))+" mA");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
..............................................................
}
public String readCurrentFile(File file) throws IOException {
InputStream input = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
input));
StringBuilder *** = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
***.append(line);
}
return ***.toString();
} finally {
input.close();
}
}
}
1.通过读取电流节点的方式,获取电流。
电流节点:”/sys/class/power_supply/battery/BatteryAverageCurrent”
2.代码:
..............................................................
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
..............................................................
public class Xxxxx extends Activity{
.....................
private static final String CHARGER_CURRENT_NOW =
"/sys/class/power_supply/battery/BatteryAverageCurrent";
..............................................................
private TextView mCurrent;
..............................................................
public void onResume(){
..............................................................
mCurrent = (TextView)findViewById(R.id.current);
..............................................................
try {
mCurrent.setText(readCurrentFile(new File(CHARGER_CURRENT_NOW))+" mA");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
..............................................................
}
public String readCurrentFile(File file) throws IOException {
InputStream input = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
input));
StringBuilder *** = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
***.append(line);
}
return ***.toString();
} finally {
input.close();
}
}
}
举报