BlackBerry - Simulate a KeyPress event

不打扰是莪最后的温柔 提交于 2019-11-26 16:38:30

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/1292896/blackberry-simulate-a-keypress-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!