Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

integration - BlackBerry - Simulate a KeyPress event

I have a BlackBerry application that needs to take pictures from the camera and send them to a server. In order to do this i invoke the native camera application and listen to the filesystem. Once an image is captured and saved as a new jpeg file i get notified, resume foreground control and go about my business. The problem starts occurring after the first time this cycle is completed because now when i decide to call the camera application again it is already opened, and now the user is seeing a thumbnail of the last picture that was taken and several buttons allowing him to manipulate/manage it. naturally what i want the user to see is a preview of what the camera is "seeing" before he snaps another photo as he did before.

I have thought of various ways to solve this including killing the camera app each time (I understand this cannot be done programatically?), sending CameraArguments when invoking the app (which appears to be useless), and now i was thinking a solution could be as simple generating a "Back" key event before switching back to my app which would theoretically dismiss the annoying edit screen. Could this really be done? and if not is there any other possible solution you may think of?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A kind of hack...

  • start Camera App
  • in TimerTask check if Camera App started and if it need to be closed (some flag)
  • if yes, invoke it(so it will became active) and push ESC keypress event injection to close it

Take a look at this:

class Scr extends MainScreen {
    boolean killCameraApp = false;
    final String mCameraModuleName = "net_rim_bb_camera";
    final CameraArguments args = new CameraArguments();

    public Scr() {
        super();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                if (isCameraRunning() && killCameraApp) {
                    getApplication().invokeAndWait(callCamera);
                    getApplication().invokeAndWait(killCamera);
                }
            }
        }, 0, 100);
    }

    Runnable callCamera = new Runnable() {
        public void run() {
            callCamera();
        }

    };

    Runnable killCamera = new Runnable() {
        public void run() {
            injectKey(Characters.ESCAPE);
            killCameraApp = false;
        }
    };

    private boolean isCameraRunning() {
        boolean result = false;
        ApplicationManager appMan = 
                ApplicationManager.getApplicationManager();
        ApplicationDescriptor[] appDes = appMan.getVisibleApplications();
        for (int i = 0; i < appDes.length; i++) {
            result = mCameraModuleName.equalsIgnoreCase(appDes[i]
                    .getModuleName());
            if (result)
                break;
        }
        return result;
    }

    private void callCamera() {
        Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, 
                new CameraArguments());
    }

    private void injectKey(char key) {
        KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
        inject.post();
    }

    protected void makeMenu(Menu menu, int instance) {
        menu.add(new MenuItem("start camera", 0, 0) {
            public void run() {
                callCamera();
                killCameraApp = false;
            }
        });
        menu.add(new MenuItem("kill app", 0, 0) {
            public void run() {
                killCameraApp = true;
            }
        });
        super.makeMenu(menu, instance);
    }
}

EDIT: Don't forget to set permissions for device release:
Options => Advanced Options => Applications => [Your Application] =>Edit Default permissions =>Interactions =>key stroke Injection


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...