开发者

Loading Page in android

How can I make loading page in androi开发者_如何学God? I want it when my application starts. Like the loading pages in games? Could You help me with that?


There are loads of examples in the internet, search by "splash screen"

http://www.anddev.org/viewtopic.php?t=815


One way of doing it would be to set your contentview in onCreate() then do all the loading in an async task and when that finishes load the 'real' layout in the onPostExecute. Check out http://developer.android.com/reference/android/os/AsyncTask.html

That way you load your "real" layout when it actually finishes loading rather than picking a generic time to switch views. This is of course assuming that you want a loading page and not a splash screen. If you want that, checkout the other answers.

Here's a quick example... Say you have a file called Hello.java .. You'd set your content view to your loading layout in OnCreate() then call this class with something like.. new DownloadFilesTask().execute(); put this private class in it...

  private class DownloadFilesTask extends AsyncTask<String, String, String> {

 protected Long doInBackground(String... params) {
     //grab stuff from the server, compute pi to 100000 places etc.
 }

 public void onPostExecute(String result) {
     //this will now switch us to our real layout, you can now do all your fancy UI stuff! :)
     setContentView(R.layout.reallayout);
 }
}

Basically this is multithreading the easy way. onPostExecute runs back on the UI thread (your main one) while doInBackground does everything on a separate thread so no black screens! I'd really urge you not to use Runnable in this situation.


You mean a splash screen right? a simple google search would reveal a lot :)

This piece of work helped me to make a splash screen.

Rather than copy and pasting the code, try to understand how he uses Threading with a time limit to achieve this target.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜