开发者

I want to show splash screen until i am done with downloading xml files from server and after completion show next screen

I 开发者_如何学编程am trying to download xml files from server when my application starts. So i want to show splash screen until am done with downloading and then show next screen. below is my code:

Here, i want to show My splash screen when getTopNotDoc() method is under execution. and after completion of that method show next screen.

//get _topics and notification document<br>
_getDoc = new ServerConnectivity(this);

public class ServerConnectivity {

private Document _questionDoc;
private Document _topics;
private Document _notifications;

public ServerConnectivity(ApplicationSession appSession){
    //getTopNotDoc();
    _this = this;
    _appSession = appSession;
            new Thread(new Runnable(){
               public void run(){   
           getTopNotDoc();
               }
            }).start();
    }
}


private void getTopNotDoc(){
    InputStream inputStream = null ;
    try{
        // Build a document based on the XML file.
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        inputStream = getClass().getResourceAsStream("topics.xml");
        _topics = builder.parse( inputStream );
        inputStream = getClass().getResourceAsStream("notification.xml");
        _notifications = builder.parse( inputStream ); 

        if(_topics == null || _notifications == null){
                Dialog.alert("Unable to connect to internet");
        }
    } 
    catch ( Exception e ){
        System.out.println( e.toString() );
    }
    finally{
        if(inputStream != null){
            try {
                inputStream.close();
            } 
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


Usually when I do this, I create a loading screen, then I just extend the Thread class.

So I would create a loading screen like this:

public class LoadingScreen extends MainScreen {
public LoadingScreen() {
    super();
    this.setTitle("loading...");
    // add a spinning animated gif or whatever here

    final Screen me = this;
    new Thread(new Runnable(){
        public void run(){   
            // do something that takes a long time
            try { Thread.sleep(1000);} catch (Exception e) {}
        }
     }){
        public void run() {
            super.run();
            synchronized (UiApplication.getEventLock()) {
                UiApplication.getUiApplication().popScreen(me);
            }
        }
    }.start();
}
}

Then I push this screen, it will perform the long task, and then pop itself when its done. (you may or may not want to disable the back button and menus on this screen)

I made the Runnable as an anonymous inner class just to compact the code, but you probably have this code already in a class somewhere else, so you would pass it in instead.


To add some flexibility and keep your classes loosely coupled together, you could make some modifications to your ServerConnectivity class so your calls could go something like the following:

// push your splash screen on to the stack
//
final SplashScreen splashScreen = new SplashScreen();
UiApplication.getUiApplication().pushScreen(splashScreen);

_getDoc = new ServerConnectivity(this, new ServerConnectivityListener() {
    public void onCompleted(ServerConnectivity sender) {

        // display next screen
        //
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                splashScreen.close();
                UiApplication.getUiApplication().pushScreen(new NextScreen());
            }
        });
    }

    public void onError(ServerConnectivity sender) {
        splashScreen.close();
        // display error message, retry, etc...
    }
 });

For this to work, you need an interface with the following definition:

public interface ServerConnectivityListener {
    void onCompleted(ServerConnectivity sender);
    void onError(ServerConnectivity sender);
}

So, your ServerConnectivity class maintains a reference to some object that implements the interface called ServerConnectivityListener This allows you to maintain loose coupling between the subject class and any observers that need to listen for events.

Within ServerConnectivity, you would make calls to the listener's methods something like this:

// begin excerpt from above...
//
if(_topics == null || _notifications == null) {
     _listener.onError(this);
} else {
    _listener.onCompleted(this);
}
catch ( Exception e ){
    System.out.println( e.toString() );
    _listener.onError(this);
//
// end excerpt from above...


Here is code for splash screen in java........after and call that view.........

http://www.randelshofer.ch/oop/javasplash/javasplash.html

import java.awt.*;
import java.awt.event.*;

public class SplashTest extends Frame implements ActionListener {
    static void renderSplashFrame(Graphics2D g, int frame) {
        final String[] comps = {"foo", "bar", "baz"};
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(130,250,280,40);
        g.setPaintMode();
        g.setColor(Color.BLACK);
        g.drawString("Loading "+comps[(frame/5)%3]+"...", 130, 260);
        g.fillRect(130,270,(frame*10)%280,20);
    }
    public SplashTest() {
        super("SplashScreen demo");
        setSize(500, 300);
        setLayout(new BorderLayout());
        Menu m1 = new Menu("File");
        MenuItem mi1 = new MenuItem("Exit");
        m1.add(mi1);
        mi1.addActionListener(this);

        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        mb.add(m1);
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }
        Graphics2D g = (Graphics2D)splash.createGraphics();
        if (g == null) {
            System.out.println("g is null");
            return;
        }
        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(200);
            }
            catch(InterruptedException e) {
            }
        }
        splash.close();
        setVisible(true);
        toFront();
    }
    public void actionPerformed(ActionEvent ae) {
        System.exit(0);
    }
    public static void main (String args[]) {
        SplashTest test = new SplashTest();
    }
}


Since,it is a thread based one,We cannot do it the normal way.So Check the following link

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

and Check whether parsing is done,Until that have the same screen,Check the condition of whehter it is downloaded or not ,and then push the screen

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜