控制J2ME的卷代码

时间:2012-07-22 22:52:58

标签: mobile java-me

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

我通过互联网找到了很多代码,但由于没有遵守程序流程图和屏幕,因此常常无法运行或出现很多错误

问候

1 个答案:

答案 0 :(得分: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;

    }


}