我编写了底层代码,用于在进行hhtp连接调用时显示activityindicator。不幸的是,在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");
但结果仍然相同。
答案 0 :(得分:1)
由于您正在使用invokeAndWait(),您是否在工作线程中执行HTTP操作?如果不是,你应该是。 ActivityIndicator应该在主调度线程中管理,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();