четвъртък, 13 януари 2011 г.

Determine if device has touch screen support in J2ME

I had to write some code to determine whether a Blackberry device supports touch screen events. There are APIs from RIM to do this but they are availble for devices with os v4.7 or later and I needed to support os 4.6 also.
So I used the pure j2me approach:
Using javax.microedition.lcdui.Canvas you can determine whether a device has touch screen support by calling the hasPointerEvents() method. Because Canvas is abstract class you should provide a dummy implementation similar to the one I offer. The following code is tested and works on actual Blackberry devices and it should also work on other devices but I haven't tested it.

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class TouchSupportInfo extends Canvas {

    private static TouchSupportInfo instance;
    
    private TouchSupportInfo(){
        
    }
    
    public static TouchSupportInfo getInstance(){
        if( instance == null ){
            instance = new TouchSupportInfo();
        }
        return instance;
    }
    
    protected void paint(Graphics g) {
        //leave empty
    }
    
    public boolean isTouchScreenDevice(){
        return hasPointerEvents();
    }

}

Няма коментари:

Публикуване на коментар