2009-06-04 28 views

回答

1

從快看看JSR規格:

(你可能想看看代碼示例,從最新的JavaME SDK,索尼愛立信開發者網站規範的附錄d本身,然後谷歌)

像往常一樣,我會擔心在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手機JSR-256實現(S60第五版)

文檔:

電池電量傳感器以下特點:

  • 數量:battery_charge

  • 語境類型:設備

  • URL:傳感器:battery_charge; contextType =裝置;模型=索尼愛立信

  • 頻道:(指數:名稱,區域單元)

  • 0:battery_charge,0-100,百分比

  • 1:charger_state,0-1,布爾

+0

非常感謝,男人! 你真的很棒! 我會試試看,並找回2你。 – Attilah 2009-06-04 16:06:46

1

你會添加javax.microedition.io.Connector.sensor到工程中的的應用程序描述符的API權限選項卡t屬性。