public synchronized void go() {
notify();
}
這樣效率比較高了!當用戶進行聯網操作的時候我們應該做一個提示界面,比如一個動畫告訴用戶正在進行聯網操作。這樣比較友好。那麼當用戶選擇聯網動作的時候,我們讓我提前做好的歡迎界面顯示在屏幕上,聯網結束後再把返回的結果顯示出來。這樣就是一個出色的聯網應用程序了。下面的這個代碼可以在屏幕上描繪一個動畫的效果,當然你也可以修改一下做成自己喜歡的樣子。
import Java.util.*;
import Javax.microedition.lcdui.*;
public class WaitCanvas
extends Canvas {
private int mCount, mMaximum;
private int mInterval;
private int mWidth, mHeight, mX, mY, mRadius;
private String mMessage;
public WaitCanvas() {
mCount = 0;
mMaximum = 36;
mInterval = 100;
mWidth = getWidth();
mHeight = getHeight();
// Calculate the radius.
int halfWidth = (mWidth - mRadius) / 2;
int halfHeight = (mHeight - mRadius) / 2;
mRadius = Math.min(halfWidth, halfHeight);
// Calculate the location.
mX = halfWidth - mRadius / 2;
mY = halfHeight - mRadius / 2;
// Create a Timer to update the display.
TimerTask task = new TimerTask() {
public void run() {
mCount = (mCount + 1) % mMaximum;
repaint();
}
};
Timer timer = new Timer();
timer.schedule(task, 0, mInterval);
}
public void setMessage(String s) {
mMessage = s;
repaint();
}
public void paint(Graphics g) {
int theta = -(mCount * 360 / mMaximum);
// Clear the whole screen.
g.setColor(255, 255, 255);
g.fillRect(0, 0, mWidth, mHeight);
// Now draw the pinwheel.
g.setColor(0, 0, 0);
g.drawArc(mX, mY, mRadius, mRadius, 0, 360);
g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
g.fillArc(mX, mY, mRadius, mRadius, theta + 270, 90);
// Draw the message, if there is a message.
if (mMessage != null)
g.drawString(mMessage, mWidth / 2, mHeight,
Graphics.BOTTOM | Graphics.HCENTER);
}
}