J2ME的控制卷代码 [英] Control Volume Code for J2ME

查看:68
本文介绍了J2ME的控制卷代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在symbian(j2me)上使用的诺基亚xpressmusic 5130 c-2的音量按钮已损坏,因此我决定制作一个j2me程序来控制音量(增加,减小)

I have nokia xpressmusic 5130 c-2 working on symbian (j2me) the volume buttons has been broken, so i decided to make a j2me program to control the volume (increase,decrease)

我在互联网上发现了很多代码,但是由于不符合程序流程图和屏幕,经常无法正常工作或出现很多错误

I have found many codes through the internet but often not work or have many errors because not complied with the program flow diagram and screen

致谢

推荐答案

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

If the device have support to the JSR 256: Mobile Sensor API, then you can use your API: http://jcp.org/en/jsr/detail?id=256

我的使用JSR 256的类:

My class that uses the 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;

    }


}

这篇关于J2ME的控制卷代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆