BlackBerry - 模拟 KeyPress 事件 [英] BlackBerry - Simulate a KeyPress event

查看:15
本文介绍了BlackBerry - 模拟 KeyPress 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BlackBerry 应用程序需要从相机拍摄照片并将它们发送到服务器.为了做到这一点,我调用本机相机应用程序并监听文件系统.一旦图像被捕获并保存为新的 jpeg 文件,我就会收到通知,恢复前台控制并开始我的业务.问题在此循环第一次完成后开始发生,因为现在当我决定再次调用相机应用程序时,它已经打开,现在用户看到最后一张照片的缩略图和几个按钮允许他操作/管理它.自然,我希望用户看到的是在他像以前一样拍摄另一张照片之前相机看到"的预览.

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.

我想了各种方法来解决这个问题,包括每次都杀死相机应用程序(我知道这不能以编程方式完成?),在调用应用程序时发送 CameraArguments (这似乎没用),现在我在想一个解决方案可以简单地生成一个Back"键事件,然后再切换回我的应用程序,理论上这会消除烦人的编辑屏幕.这真的可以做到吗?如果没有,您可能会想到其他可能的解决方案吗?

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?

推荐答案

一种hack...

  • 启动相机应用
  • 在 TimerTask 中检查 Camera App 是否启动以及是否需要关闭(一些标志)
  • 如果是,调用它(这样它就会激活)并按下 ESC 按键事件注入来关闭它

看看这个:

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);
    }
}

不要忘记设置设备发布权限:
Options => Advanced Options => Applications => [Your Application] =>Edit Default permissions =>Interactions =>key stroke Injection

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

这篇关于BlackBerry - 模拟 KeyPress 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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