如何使用 JSR 256 检测电源线何时从电源插座中拔出?
问问题
1810 次
2 回答
1
快速浏览一下 JSR 的规范:
(您可能想查找代码示例,从规范本身的附录 D、最新的 JavaME SDK、索尼爱立信开发者网站开始,然后是 google)
与往常一样,我会担心 JSR 的不同实现中的碎片化,但这是我的第一个想法:
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 权限。
- - - -编辑 - - -
索尼爱立信 Satio 电话(S60 第 5 版)上的 JSR-256 实施文档:
电池充电传感器具有以下特点:
数量:battery_charge
上下文类型:设备
URL:传感器:battery_charge;contextType=device;model=SonyEricsson
渠道:(索引:名称、范围、单位)
0:电池充电,0-100,百分比
1:充电器状态,0-1,布尔值
于 2009-06-04T15:18:05.707 回答
1
您可以将 javax.microedition.io.Connector.sensor 添加到项目属性的 Application Descriptor 的 API Permissions 选项卡中。
于 2010-05-05T14:38:14.330 回答