0

我已经编写了底层代码以在进行 hhtp 连接调用时显示活动指示器。不幸的是,在 http 连接调用完成之前,活动指示器不会出现在屏幕上。因此,它没有达到它的目的。

UiApplication.getUiApplication().invokeAndWait(new Runnable()
                {
                  public void run()
                  {

                       ActivityIndicatorView view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
                        ActivityIndicatorModel model = new ActivityIndicatorModel();
                        ActivityIndicatorController controller = new ActivityIndicatorController();

                        view.setController(controller);
                        view.setModel(model);

                        controller.setModel(model);
                        controller.setView(view);

                        model.setController(controller);        

                        Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
                        view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

                        _activityFieldManager.add(view);
                          UiApplication.getUiApplication().getActiveScreen().invalidate();

                  }
                });
                //SyncWithDB is the Class that makes the Http connection call
                new SyncWithDB();
                _activityFieldManager.delete(view);
                UiApplication.getUiApplication().getActiveScreen().invalidate();
                Dialog.inform("Sync Complete");

正如建议的那样,我什至尝试过

view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
                ActivityIndicatorModel model = new ActivityIndicatorModel();
                ActivityIndicatorController controller = new ActivityIndicatorController();

                view.setController(controller);
                view.setModel(model);

                controller.setModel(model);
                controller.setView(view);

                model.setController(controller);        

                Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
                view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

                _activityFieldManager.add(view);
                  UiApplication.getUiApplication().getActiveScreen().invalidate();

                UiApplication.getUiApplication().invokeAndWait(new Runnable()
                {
                  public void run()
                  {

                      try {
                        new SyncWithDB();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                  }
                });


                _activityFieldManager.delete(view);
                UiApplication.getUiApplication().getActiveScreen().invalidate();
                Dialog.inform("Sync Complete");

但结果还是一样。

4

1 回答 1

1

由于您使用的是 invokeAndWait(),您是否在工作线程中执行 HTTP 操作?如果不是,你应该是。ActivityIndi​​cator 应该在主调度线程中管理,HTTP 操作应该在工作线程中管理。

例如:

view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
ActivityIndicatorModel model = new ActivityIndicatorModel();
ActivityIndicatorController controller = new ActivityIndicatorController();

view.setController(controller);
view.setModel(model);

controller.setModel(model);
controller.setView(view);

model.setController(controller);        

Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

_activityFieldManager.add(view);
UiApplication.getUiApplication().getActiveScreen().invalidate();

Thread thread = new Thread()
{
  public void run()
  {
    try {
      new SyncWithDB();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    UiApplication.getUiApplication().invokeLater(
      new Runnable()
      {
        protected void run()
        {
          _activityFieldManager.delete(view);
          UiApplication.getUiApplication().getActiveScreen().invalidate();
          Dialog.inform("Sync Complete");
        }
      }
    );
  }
}
thread.start();

或者,更笼统一点:

ActivityIndicatorView view;
ActivityFieldManager _activityFieldManager;

class SyncFinished extends Runnable
{
  public void run()
  {
    _activityFieldManager.delete(view);
    UiApplication.getUiApplication().getActiveScreen().invalidate();
    Dialog.inform("Sync Complete");
  }
}

class SyncThread extends Thread
{
  Runnable runWhenFinished;

  public SyncThread(Runnable r)
  {
    super();
    runWhenFinished = r;
  }

  public void run()
  {
    try {
      new SyncWithDB();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    if (runWhenFinished != null)
      UiApplication.getUiApplication().invokeLater(runWhenFinished);
  }
}

view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
ActivityIndicatorModel model = new ActivityIndicatorModel();
ActivityIndicatorController controller = new ActivityIndicatorController();

view.setController(controller);
view.setModel(model);

controller.setModel(model);
controller.setView(view);

model.setController(controller);        

Bitmap bitmap = Bitmap.getBitmapResource("spinner.png");
view.createActivityImageField(bitmap, 5, Field.FIELD_HCENTER);

_activityFieldManager.add(view);
UiApplication.getUiApplication().getActiveScreen().invalidate();

SyncThread thread = new SyncThread(new SyncFinished());
thread.start();
于 2011-10-19T18:30:06.947 回答