黑莓请等待时间筛选出 [英] BlackBerry Please Wait Screen with Time out

查看:177
本文介绍了黑莓请等待时间筛选出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想创建一个请稍候但屏幕画面时,我的程序从Web服务请求的数据将出现,并且会隐藏在这个过程finished.Also我想补充一个超时,如果请求过程持续时间超过90秒。

任何人都可以帮助或告诉我关于这个问题的指导例子。

 公共静态无效showBusyDialog(){
    尝试
    {
        如果(busyDialog == NULL){            busyDialog =新的对话框(请等待,NULL,NULL,0,Bitmap.get predefinedBitmap(Bitmap.HOURGLASS));
            busyDialog.setEscapeEnabled(假);
        }
        同步(Application.getEventLock()){
            busyDialog.show();
        }
    }
    赶上(例外五)
    {    }
}

和我藏身code是

 公共静态无效hideBusyDialog(){
    尝试
    {
        如果(busyDialog == NULL){
      // busyDialog =新的对话框(请稍候...,NULL,NULL,0,Bitmap.get predefinedBitmap(Bitmap.HOURGLASS));
        busyDialog.setEscapeEnabled(假);
        }
        同步(Application.getEventLock()){
            busyDialog.close();
        }    }赶上(例外五)
    {    }
}


解决方案

许多BlackBerry®智能手机应用程序需要等待一些网络活动(或其他阻塞操作,必须在后台处理),同时还阻碍了用户界面(UI),并显示一个进度指示器。

您可以通过这个链接遵循

链接

<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/ta-p/493808\"相对=nofollow>样本请等待屏幕 - 第1部分

<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-2/ta-p/510363\"相对=nofollow>样本请等待屏幕 - 第2部分

<一个href=\"http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-Screen-part-3/ta-p/516996\"相对=nofollow>样本请等待屏幕 - 第4部分

您可以下载简单的例子为请等待屏幕

PleaseWait1.zip 25 KB

PleaseWait2.zip 25 KB

PleaseWait3.zip 25 KB

注意:在上面的情况下不链接然后工作只要按照如下内容

有似乎是在编程时,两个常见问题:

1)由于应用程序不能阻止事件线程,他们如何获得UI处理等吗?
    2)后台线程如何更新UI?

这篇文章的目的是帮助这些问题,并提供了一​​个功能齐全的请等待样本弹出式屏幕。但是,因为有相当多的解释,这第一篇文章中,我们将只创建一个弹出式屏幕,将显示本身,托起UI,然后一旦后台处理已完成自身删除。这不会给我们任何进度指示,也没有让用户取消等待。这些点会在后续文章中介绍。但本文提供的code将是有益的,无论如何,尤其是当后台处理的持续时间是未知的,并且用户可能不取消该处理。

首先,我们先从我们需要运行的后台处理。虽然这可以是任何东西,通常这将是网络处理,如下所示:

  httpConn =(HttpConnection的)Connector.open(_url +; deviceside =真正的);
响应code = httpConn.getResponse code();
responseMessage =响应code:+ Integer.toString(响应code);

要启动该网络处理,我们有一个包含MainScreen

1)BasicEditField,允许URL的条目

2)应显示响应code(或错误信息RichTextField)。以下是该屏幕的重要组成部分:

  BasicEditField _requestedURLField =新BasicEditField(HTTP://,www.blackberry.com,255,BasicEditField.FILTER_URL);
RichTextField _responseField =新RichTextField(&LT;响应code&gt;中,RichTextField.NON_FOCUSABLE);

我们愿与结果进行更新MainScreen。如上所述,背景处理不能直接更新UI; UI更新code必须是在事件线程。有几种方法可以得到一个后台进程到事件线程,请参阅相关文章了解更多。在这种情况下,我们会使用以下code:

  //让事情最终的,所以我们可以在内部类中使用它们
最后弦乐textString = responseMessage;
最后RichTextField RTF = _resultField;
UiApplication.getUiApplication()的invokeLater(Runnable的新(){
    公共无效的run(){
        rtf.setText(textString);
    }
});

现在我们必须定义PleaseWaitPopupScreen,同时等待将要显示。

要给用户的东西来看待,而他们在等待,我们有一个动画.gif注意,这是使用在AnimatedGIFField的code(见相关链接)diplayed。和,因此用户知道他们正在等待时,PleaseWaitPopupScreen与字符串来显示提供,如下面的构造示出了:

 私人PleaseWaitPopupScreen(字符串文本){
    超(新VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
    GIFEn codeDIMAGE ourAnimation =(GIFEn codeDIMAGE)GIFEn codedImage.getEn codedImageResource(cycle.agif);
    _ourAnimation =新AnimatedGIFField(ourAnimation,Field.FIELD_HCENTER);
    this.add(_ourAnimation);
    _ourLabelField =新的LabelField(文字,Field.FIELD_HCENTER);
    this.add(_ourLabelField);
}

PleaseWaitPopupScreen提供了一个方法 - showScreenAndWait(..) - 这将创建并显示弹出式屏幕,运行的后台处理,然后关闭该弹出式屏幕。

拼图的最后一块涉及与运行处理提供showScreenAndWait(..)。

Java有一个Runnable,这是包含应执行的公共无效的run()方法的对象的概念。在这种情况下,我们有连接code和屏幕更新code,上面给出的,应该执行。所以,这个code为打包成一个新的可运行的对象,它被提供给showScreenAndWait(..)。这里是该方法。注意,如何创建一个新的线程并运行。

 公共静态无效showScreenAndWait(最终的Runnable runThis,字符串文本){
    最后PleaseWaitPopupScreen thisScreen =新PleaseWaitPopupScreen(文本);
    螺纹threadToRun =新主题(){
        公共无效的run(){
            //首先,显示该屏幕
            UiApplication.getUiApplication()的invokeLater(Runnable的新(){
                公共无效的run(){
                    。UiApplication.getUiApplication()pushScreen(thisScreen);
                }
            });
            //现在运行code必须在后台执行
            尝试{
                runThis.run();
            }赶上(的Throwable t)的{
                t.printStackTrace();
                抛出新的RuntimeException(在等待检测到异常:+ t.toString());
            }
            //现在关闭此屏幕
            UiApplication.getUiApplication()的invokeLater(Runnable的新(){
                公共无效的run(){
                    。UiApplication.getUiApplication()popScreen(thisScreen);
                }
            });
        }
    };
    threadToRun.start();
}

这是PleaseWaitPopupScreen的重要组成部分。注意:此code如何创建和显示弹出窗口给用户,包括动画图标,而它正在运行的后台处理。来自用户的输入被弹出画面,直到处理完成阻塞。始发屏幕被更新作为背景处理的结果。

下载相关的.zip文件,其中包含本文中包含的源代码。

在接下来的文章中,我们将扩展该code到能够处理:

a)由后台线程的状态更新

二)的时间去指示

c)以黑莓智能手机用户被撤消

Hello I am trying to create a please wait screen.This screen will appear when my program requests data from web service and will hide when the process is finished.Also I want to add a time out if request process lasts longer than 90 seconds.

can anyone help or show me a guiding example about that matter.

public static void showBusyDialog() {
    try
    {
        if (busyDialog == null) {

            busyDialog = new Dialog("Please Wait", null, null, 0, Bitmap.getPredefinedBitmap(Bitmap.HOURGLASS));
            busyDialog.setEscapeEnabled(false);
        }
        synchronized (Application.getEventLock()) {
            busyDialog.show();
        }
    }
    catch(Exception e)
    {

    }
}

and my hiding code is

public static void hideBusyDialog() {
    try
    {
        if (busyDialog == null) {
      //  busyDialog = new Dialog("Please wait...", null, null, 0, Bitmap.getPredefinedBitmap(Bitmap.HOURGLASS));
        busyDialog.setEscapeEnabled(false);
        }
        synchronized (Application.getEventLock()) {
            busyDialog.close();
        }

    }catch(Exception e)
    {

    }
}

解决方案

Many BlackBerry® smartphone applications need to wait for some network activity (or another blocking operation, which must process in the background), while still holding up the User Interface (UI) and displaying a progress indicator.

You can follow through this links

Links

Sample "Please Wait" screen - part 1

Sample "Please Wait" screen - part 2

Sample "Please Wait" screen - part 4

you can download simple examples for Please wait screen

PleaseWait1.zip 25 KB

PleaseWait2.zip 25 KB

PleaseWait3.zip 25 KB

Note :in case above Links not working then just follow following contents

There seem to be two common issues when programming this:

1) As applications are not allowed to block the Event Thread, how do they get the UI processing to wait? 2)How can the background Thread update the UI?

This article is intended to help with these issues and provide a fully functioning "Please Wait" sample Popup Screen. However, as there is quite a lot to explain, in this first article, we will just create a popup screen that will show itself, hold up the UI, and then remove itself once the background processing has finished. This does not give us any progress indication, nor does it let the user cancel the wait. These points will be covered in a followup article. But the code supplied with this article will be useful anyway, especially when the duration of the background processing is not known and the user may not cancel the processing.

First, we start with the background processing we need to run. While this could be anything, typically this will be network processing, like the following:

httpConn = (HttpConnection)Connector.open(_url + ";deviceside=true");
responseCode = httpConn.getResponseCode();
responseMessage = "Response Code: " + Integer.toString(responseCode);

To initiate this network processing, we have a MainScreen that contains

1) A BasicEditField that allows the entry of a URL

2) A RichTextField that should display the response code (or error message). Here are the important parts of that screen:

BasicEditField _requestedURLField = new BasicEditField("http://", "www.blackberry.com", 255, BasicEditField.FILTER_URL);
RichTextField _responseField = new RichTextField("<response code>", RichTextField.NON_FOCUSABLE); 

We would like the MainScreen to be updated with the result. As noted above, background processing can't directly update the UI; UI updating code must be on the Event Thread. There are several ways to get a background process onto the Event Thread, see the related article for more. In this case, we will use the following code:

// Make things final so we can use them in the inner class
final String textString = responseMessage;
final RichTextField rtf = _resultField;
UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() { 
        rtf.setText(textString);
    }
});

Now we must define the PleaseWaitPopupScreen to be displayed while waiting.

To give the user something to look at while they are waiting, we have an animated .gif, which is diplayed using the code in the AnimatedGIFField (see related link). And, so the user knows what they are waiting for, the PleaseWaitPopupScreen is supplied with a String to display, as the following constructor shows:

private PleaseWaitPopupScreen(String text) {
    super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
    GIFEncodedImage ourAnimation = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("cycle.agif");
    _ourAnimation = new AnimatedGIFField(ourAnimation, Field.FIELD_HCENTER);
    this.add(_ourAnimation);
    _ourLabelField = new LabelField(text, Field.FIELD_HCENTER);
    this.add(_ourLabelField);
}

PleaseWaitPopupScreen provides a method – showScreenAndWait(..) – which will create and display the Popup screen, run the Background processing, and then dismiss the Popup screen.

The final piece of the puzzle involves supplying showScreenAndWait(..) with the processing to run.

Java has the concept of a Runnable, which is an Object that contains a public void run() method that should be executed. In this case, we have the Connection code and screen update code, given above, that should be executed. So, this code is packaged up into a new Runnable Object, which is supplied to showScreenAndWait(..). And here is that method. Note how a new Thread is created and run.

public static void showScreenAndWait(final Runnable runThis, String text) {
    final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(text);
    Thread threadToRun = new Thread() {
        public void run() {
            // First, display this screen
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    UiApplication.getUiApplication().pushScreen(thisScreen);
                }
            });
            // Now run the code that must be executed in the Background
            try {
                runThis.run();
            } catch (Throwable t) {
                t.printStackTrace();
                throw new RuntimeException("Exception detected while waiting: " + t.toString());
            }
            // Now dismiss this screen
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    UiApplication.getUiApplication().popScreen(thisScreen);
                }
            });
        }
    };
    threadToRun.start();
}

And this is the key part of the PleaseWaitPopupScreen. Note how this code will create and display a Popup screen to the user, including an animated icon, while it is running the background processing. Input from the user is blocked by the Popup screen until the processing completes. The originating screen is updated as a result of the background processing.

Download the associated .zip archive, which contains the source included in this article.

In the next article, we will extend this code to be able to handle:

a) Status updates from the Background Thread

b) "Time to go" indication

c) Being cancelled by the BlackBerry smartphone user

这篇关于黑莓请等待时间筛选出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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