JSR 256电池事件 [英] JSR 256 battery events
问题描述
使用JSR 256从电源插座上拔下电源线时,如何检测?
How can I detect whenever the power cord is unplugged from electrical socket using JSR 256?
推荐答案
快速浏览一下JSR的规范:
From a quick look at the specifications of the JSR:
(您可能要查找代码示例,从规范本身的附录D,最新的JavaME SDK,索尼爱立信开发者网站,再到Google)
(you might want to look for code examples, starting with Appendix D of the spec itself, the latest JavaME SDK, Sony Ericsson developer website, then google)
和往常一样,我会担心JSR的各种实现中的碎片化,但这是我的第一个想法:
As always, I would be worried about fragmentation in the diverse implementations of the JSR, but here's my first idea:
import javax.microedition.sensor.*;
SensorInfo[] powerSensorInfoArray = SensorManager.findSensors("power","ambient");
//let's assume there is one SensorInfo in the array.
//open a connection to the sensor.
SensorConnection connection = (SensorConnection)Connector.open(powerSensorInfoArray[0].getUrl(), Connector.READ);
// add a DataListener to the connection
connection.setDataListener(new MyDataListener(), 1);
// implement the data listener
public class MyDataListener implements DataListener {
public void dataReceived(SensorConnection aSensor, Data[] aDataArray, boolean isDataLost) {
//let's assume there is only one channel for the sensor and no data was lost.
// figure out what kind of data the channel provides.
int dataType = aDataArray[0].getChannelInfo().getDataType();
//now, I suggest you switch on dataType and print the value on the screen
// experimentation on the JSR256 implementation you're targetting seems to be
// the only way to figure out out power data is formatted and what values mean.
//only one of the following 3 lines will work:
double[] valueArray = aDataArray[0].getDoubleValues();
int[] valueArray = aDataArray[0].getIntValues();
Object[] valueArray = aDataArray[0].getObjectValues();
// let's assume one value in the valueArray
String valueToPrint = "" + valueArray[0];
// see what happens with that and you plug or unplug the power supply cable.
}
}
您需要将javax.microedition.io.Connector.sensor
添加到您的MIDlet权限.
You'll need to add javax.microedition.io.Connector.sensor
to your MIDlet permissions.
-------编辑------
-------EDIT------
来自Sony-Ericsson Satio手机(S60第5版)的JSR-256实现的文档:
Documentation from the JSR-256 implementation on Sony-Ericsson Satio phone (S60 5th edition):
电池电量传感器具有以下特征:
The battery charge sensor has the following characteristics:
-
数量:电池电量
Quantity: battery_charge
上下文类型:设备
URL:sensor:battery_charge; contextType = device; model = SonyEricsson
URL: sensor:battery_charge;contextType=device;model=SonyEricsson
渠道:(索引:名称,范围,单位)
Channels: (index: name, range, unit)
0:电池电量,0-100%,百分比
0: battery_charge, 0-100, percent
1:charger_state,0-1,布尔值
1: charger_state, 0-1, boolean
这篇关于JSR 256电池事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!