Android SplashScreen

2019-01-01 02:44发布

问题:

I\'m developing an application which basically downloads a lot of data at the start of the application itself and displays it in the ListActivity. What I\'m planning to do is show a Splash Screen till the data is loaded.

Till now all my attempts have been futile. I tried anddev.org mentioned methods, but my problem is that the main Activity should start but The Splash Screen should be visible till I populate my ListActivity. So in short I have to go through the following steps:

  1. Start my main activity.
  2. Show the Splash Screen.
  3. Keep running the process in background.
  4. Exit the Splashscreen when processing completed and show the main List.

Hope you understand what it is like....

回答1:

The problem is most likely that you are running the splash screen (some sort of Dialog such as ProgressDialog I assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTask to go download all your data, then hide the splash screen once the task is complete.

So your Activity\'s onCreate() method would simply create a ProgressDialog and show it. Then create the AsyncTask and start it. I would make the AsyncTask an inner class of your main Activity, so it can store the data it has downloaded to some variable in your Activity and close the ProgressDialog in its onPostExecute() method.

Not sure how to elaborate anymore without just showing the code, so here it is:

public class MyActivity extends Activity {
    private ProgressDialog pd = null;
    private Object data = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Show the ProgressDialog on this thread
        this.pd = ProgressDialog.show(this, \"Working..\", \"Downloading Data...\", true, false);

        // Start a new thread that will download all the data
        new DownloadTask().execute(\"Any parameters my download task needs here\");
    }

    private class DownloadTask extends AsyncTask<String, Void, Object> {
         protected Object doInBackground(String... args) {
             Log.i(\"MyApp\", \"Background thread starting\");

             // This is where you would do all the work of downloading your data

             return \"replace this with your data object\";
         }

         protected void onPostExecute(Object result) {
             // Pass the result data back to the main activity
             MyActivity.this.data = result;

             if (MyActivity.this.pd != null) {
                 MyActivity.this.pd.dismiss();
             }
         }
    }    
}

Obviously there are some pieces you need to fill in there, but this code should run and give you a good starting point (forgive me if there is a code error, I don\'t have access to the Android SDK as I\'m typing this currently).

Some more good reading on the subject of AsyncTasks in Android can be found here and here.



回答2:

just for reference this is the best way I found to make a splash screen: http://android-developers.blogspot.de/2009/03/window-backgrounds-ui-speed.html

I was searching for this for quite a while, from androids docs.. if you want to avoid those black screens, you need to create a theme with windowBackground so:

<resources>
    <style name=\"Theme.Shelves\" parent=\"android:Theme\">
        <item name=\"android:windowBackground\">@drawable/background_shelf</item>
        <item name=\"android:windowNoTitle\">true</item>
    </style>
</resources>

And set this theme as the theme for your main activity... TADA, splashscreen from the first second.

If you want a complex background and not just an image that will be stretched to fill you can use Drawables, here is an example of a layer-list that will keep the logo centered with a black background:

<layer-list xmlns:android=\"http://schemas.android.com/apk/res/android\" >

    <item android:drawable=\"@color/black\">
    </item>
    <item>
        <bitmap
            android:gravity=\"center\"
            android:src=\"@drawable/logo\"
            android:tileMode=\"disabled\" >
        </bitmap>
    </item>
</layer-list>


回答3:

Splash screen example :

public class MainActivity extends Activity {
    private ImageView splashImageView;
    boolean splashloading = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        splashImageView = new ImageView(this);
        splashImageView.setScaleType(ScaleType.FIT_XY);
        splashImageView.setImageResource(R.drawable.ic_launcher);
        setContentView(splashImageView);
        splashloading = true;
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            public void run() {
                splashloading = false;
                setContentView(R.layout.activity_main);
            }

        }, 3000);

    }

}


回答4:

  1. Start my main activity.
  2. Show the Splash Screen.
  3. Keep running the process in background.
  4. Exit the Splashscreen when processing completed and show the main List.

I tried this way, but the problem is; it will show the main activity before starting the splash screen activity.

I made it this way:

  1. Start the Splash screen
  2. When process gets completed, start the \"main activity\"
  3. Do Not forget to handle the back button, so it should close the App ist will be pressed in the main activity. Otherwise will back to the splash screen (loop)

My Problem is how to disable \"show the Menu of the Splash screen activity\" by pressing the menu-button.

Edit:

Disable show menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // getMenuInflater().inflate(R.menu.activity_main, menu);
    return false;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {       
    // return super.onMenuItemSelected(featureId, item);
    return false;
}