Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java ME (J2ME) > Зарядка телефона


Автор: 404NotFound 3.9.2012, 13:45
Можно ли как то определить, подключена ли к телефону зарядка, или он работает от аккумулятора?

Автор: tweener 11.9.2012, 16:48
JavaME: JSR-256 (Mobile Sensor API): charger_state

http://www.developer.nokia.com/Community/Wiki/How_to_use_sensors_in_Java_ME

Цитата

The code sample below shows, how to search a sensor of desired quantity. The method also gets the sensor URL and returns the correct SensorConnection to it. 
Код

/**
     * Searches sensors of desired quantity and if found, returns a
     * SensorConnection opened to it.
     * @param quantity the sensor quantity, for example "acceleration", "battery_charge", 
     * "charger_state" and "network_field_intensity" 
     * @return SensorConnection, which has been opened to a sensor matching the criteria
     */
    private SensorConnection openSensor(String quantity) {
    infos = SensorManager.findSensors(quantity, null);
    if (infos.length==0) return null;
        String sensor_url = infos[0].getUrl();
    try {
     return (SensorConnection)Connector.open(sensor_url);
    }catch (IOException ioe) {
            ioe.printStackTrace();
     return null;
    }
    }

The following method shows, how to set DataListener for listening sensor values from the three sensors: 
Код

/**
     * Initializes (opens) the sensor connections and sets the DataListener.
     * Takes also care of removing the DataListeners and closing the connections.
     */
    private synchronized void initSensors() {
        batterySensor = openSensor(BATTERY);
    if (batterySensor == null) return;
        chargeSensor = openSensor(CHARGER);
    if (chargeSensor == null) return;
        networkSensor = openSensor(NETWORK);
    if (networkSensor == null) return;
        try {
     batterySensor.setDataListener(this, BUFFER_SIZE);
     chargeSensor.setDataListener(this, BUFFER_SIZE);
     networkSensor.setDataListener(this, BUFFER_SIZE);
     while(!isStopped){
         try{
      wait();
  }catch(InterruptedException ie){}
     }
     batterySensor.removeDataListener();
     chargeSensor.removeDataListener();
     networkSensor.removeDataListener();
    }catch (IllegalMonitorStateException imse) {
            imse.printStackTrace();
        }catch (IllegalArgumentException iae) {
            iae.printStackTrace();
        }
    try {
         batterySensor.close();
     chargeSensor.close();
     networkSensor.close();
    } catch(IOException ioe){
     ioe.printStackTrace();
    }
        if (isStopped) {
            batterySensor = null;
            chargeSensor = null;
            networkSensor = null;
        }
    }

Finally, the dataReceived() method is implemented for getting the sensor values. The method creates the Strings for showing the sensor values properly formatted. 
Код

/**
     * Notification of the received sensor data.
     * @param sensor - SensorConnection, the origin of the received data
     * @param data - the received sensor data
     * @param isDataLost - true if some data has been lost 
     */
    public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost) {
        sensorString = sensor.getSensorInfo().getQuantity();
        int values[] = data[0].getIntValues();
        if (sensorString.equals(BATTERY)) {
            batteryString = "" + values[0] + "%";
        }
        else if (sensorString.equals(CHARGER)) {
            int value = values[0];
            if (value == 0) chargeString = "not plugged in";
            else if (value == 1) chargeString = "plugged in";
        }
        else if (sensorString.equals(NETWORK)) {
            networkString = "" + values[0] + "%";
        }
        repaint();
    }



http://www.developer.nokia.com/Community/Wiki/images/7/7d/SensorTest2.zip?20090522114827

Автор: 404NotFound 15.9.2012, 22:35
Спасибо!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)