1

我有 nokia xpressmusic 5130 c-2 在 symbian (j2me) 上工作,音量按钮坏了,所以我决定制作一个 j2me 程序来控制音量(增加,减少)

我通过互联网找到了很多代码,但由于不符合程序流程图和屏幕,经常无法正常工作或有很多错误

问候

4

1 回答 1

1

如果设备支持 JSR 256:Mobile Sensor API,那么您可以使用您的 API:http: //jcp.org/en/jsr/detail ?id=256

我使用 JSR 256 的课程:

/*
 * REVISION HISTORY:
 *
 * Date         Author(s)
 * CR           Headline
 * =============================================================================
 * 22/Oct/2009  Douglas Daniel Del Frari
 * <CR51674>    Initial Version             
 * =============================================================================
 * 25/Feb/2010  Douglas Daniel Del Frari
 * <CR52577>    Added more one sensor (charge state) to detection of charger state.             
 * =============================================================================
 */

package j2me.mobilesensor;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.sensor.Data;
import javax.microedition.sensor.DataListener;
import javax.microedition.sensor.SensorConnection;
import javax.microedition.sensor.SensorInfo;
import javax.microedition.sensor.SensorManager;

/**
 * This class uses the resource of the mobile sensor (JSR-256) to capture the
 * phone battery level, and the volume level of the phone
 * 
 * @author Douglas D. Del Frari (douglas.frari@gmail.com)
 */
public class MobileSensorManager implements DataListener, Runnable{
    /** ID for the alert dialog */
    public static final int ID_DIALOG_BATTERY_CHARGE = 90;

    /**
     * String used to get the battery charge level. 
     */
    private static final String BATTERY = "battery_charge";

    /**
     * String used to get the charger state (plugged on or off). 
     */
    private static final String BATTERY_CHARGER_STATE = "charger_state";

    /**
     * String used to get the sound level 
     */
    private static final String SOUND_LEVEL = "sound_level_setting";

    // sensors 
    private static SensorConnection batterySensor = null;
    private SensorConnection soundSensor = null;

    // SensorInfo objects containing info about
    private SensorInfo infos[]; 

    // Is sensor thread running?
    private static boolean isStopped = false; 

    // Buffer for the sensor data
    private static final int BUFFER_SIZE = 1;

    /**
     * Indicate the minimal value of battery level of the game
     */
    public static final int BATTERY_LIFE_LIMIT = 25;


    // Thread for initializing and listening
    private Thread thread = null; 

    /*
     * Sensor quantity string received from the dataReceived() method
     */
    private String sensorString = ""; 

    // Sensor value (battery_charge)
    private String batteryString = "";
    private String volumeString = "";


    private boolean isActiveBatterySensor; 
    private boolean isLowBatteryCharge;
    private int batteryChargeValue;
    private int volumeValue;

    private SensorConnection batteryChargerState;

    private boolean chargeState;


    // instance this class
    private static MobileSensorManager instance;

    /**
     * default constructor
     */
    private MobileSensorManager() {
    }

    /**
     * Get the MobileSensorManager instance
     * 
     * @return instance this
     */
    public static MobileSensorManager getInstance() {
        if (instance == null) {
            instance = new MobileSensorManager();
        }

        return instance;
    }

    /**
     * @param stopped
     */
    private synchronized void setStopped(boolean stopped) {
        isStopped = stopped;
        notify();
        if (thread != null)
            thread = null;
    }

    /**
     * start the mobile sensors
     */
    public synchronized void start() {
        setStopped(false);
        if (thread == null)
            thread = new Thread(this);
        thread.start();
    }

    /**
     * stop the mobile sensors
     */
    public synchronized  void stop() {
        setStopped(true);
        thread = null;
    }

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        initSensors();
    }

    /**
     * Initializes (opens) the sensors 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) {
            isActiveBatterySensor = false;
            return;
        } else {
            isActiveBatterySensor = true;
        }

        batteryChargerState = openSensor(BATTERY_CHARGER_STATE);
        soundSensor = openSensor(SOUND_LEVEL);


        try {
            batterySensor.setDataListener(this, BUFFER_SIZE);
            if (soundSensor !=null) {
                soundSensor.setDataListener(this, BUFFER_SIZE);
            }


            if (batteryChargerState != null) {
                batteryChargerState.setDataListener(this, BUFFER_SIZE);
            }

            while (!isStopped) {
                try {
                    wait();
                } catch (InterruptedException ie) {
                }
            }
            batterySensor.removeDataListener();

            if (soundSensor !=null) {
                soundSensor.removeDataListener();
            }

            if (batteryChargerState != null) {
                batteryChargerState.removeDataListener();
            }

        } catch (IllegalMonitorStateException imse) {
            imse.printStackTrace();
        } catch (IllegalArgumentException iae) {
            iae.printStackTrace();
        }
        try {
            if (batterySensor!=null) {
                batterySensor.close();
            }
            if (soundSensor !=null) {
                soundSensor.close();
            }

            if (batteryChargerState != null) {
                batteryChargerState.close();
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        if (isStopped) {
            batterySensor = null;
            soundSensor = null;
            batteryChargerState = null;
        }
    }

    /**
     * Searches sensors of desired quantity and if found returns a
     * SensorConnection opened to it.
     * 
     * @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;
        }
    }

    /* (non-Javadoc)
     * @see javax.microedition.sensor.DataListener#dataReceived(javax.microedition.sensor.SensorConnection, javax.microedition.sensor.Data[], boolean)
     */
    public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost) {

        sensorString = sensor.getSensorInfo().getQuantity();
        int values[] = data[0].getIntValues();
        if (sensorString.equals(BATTERY)) {
            setBatteryString("" + values[0] + "%");
            batteryChargeValue = values[0];
        }

        if (sensorString.equals(SOUND_LEVEL)) {
            setVolumeString("" + values[0] + " sound level");
            volumeValue = values[0];
        }

        if (sensorString.equals(BATTERY_CHARGER_STATE)) {
            int value = values[0];
            if (value == 0)
                chargeState = false;
            else if (value == 1)
                chargeState = true;
        }

        if (values[0] <= BATTERY_LIFE_LIMIT) {
            isLowBatteryCharge = true;
        } else {
            isLowBatteryCharge = false;
        }

    }



    /**
     * @return the batteryString
     */
    public String getBatteryString() {
        return batteryString;
    }



    /**
     * @param batteryString the batteryString to set
     */
    public void setBatteryString(String batteryString) {
        this.batteryString = batteryString;
    }



    /**
     * @return the isLowBatteryCharge
     */
    public boolean isLowBatteryCharge() {
        return isLowBatteryCharge;
    }

    /**
     * @return the isActiveBatterySensor
     */
    public boolean isActiveBatterySensor() {
        return isActiveBatterySensor;
    }

    /**
     * @return the batteryChargeValue
     */
    public int getBatteryChargeValue() {
        return batteryChargeValue;
    }

    /**
     * @param volumeString the volumeString to set
     */
    public void setVolumeString(String volumeString) {
        this.volumeString = volumeString;
    }

    /**
     * @return the volumeString
     */
    public String getVolumeString() {
        return volumeString;
    }



    /**
     * @param volumeValue the volumeValue to set
     */
    public void setVolumeValue(int volumeValue) {
        this.volumeValue = volumeValue;
    }

    /**
     * @return the volumeValue
     */
    public int getVolumeValue() {
        return volumeValue;
    }

    /**
     * Get state of battery charge
     * 
     * @return True if the charge is plugged in, otherwise not plugged in
     */
    public boolean isChargedState() {

        return chargeState;

    }


}
于 2012-07-23T15:28:37.637 回答