AsyncTask运行时的Android Splashscreen

时间:2011-12-28 09:52:24

标签: android splash-screen

目前我正在开发一个android-phonegap项目。

在应用程序加载index.html之前,有一些本机代码正在运行。详细说明是AsyncTask中的SD卡(如果可用)。

我设法在A.Task工作时显示进度条,但此外我想在后台添加一个Splash屏幕。我主要使用Phonegap,只是从本机代码开始。所以我对所有这些布局,主题以及您在.xml文件中定义的其他内容感到困惑。我非常确定这对于更大的UI设计也是一个好主意,但对于我现在想要的简单的Splash屏幕,它感觉就像一个完全矫枉过正。

这是来自源代码的片段。直接前进。 onCreate()调用AsyncTask做一些工作并在它的PostExecute方法中启动PhoneGap。我希望屏幕出现在onCreate方法或onPreExecute中。作业完成后,屏幕上我将关闭onPostExecute()中的屏幕。我还添加了评论来说明我的想法。

public class myAct extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Show Splash here or in onPreExecute()!
        new myAsTa().execute();
    }
}


class myAsTa extends AsyncTask<Void, Integer, Boolean> {
    ProgressDialog dialog = new ProgressDialog(myAct.this);

    @Override
    protected void onPreExecute() {
        //or show splash here!
        dialog.setMessage("Cool Progressbar!");
        dialog.show();
        super.onPreExecute();
    }

    protected Boolean doInBackground(Void... values) {
            //magician at work!
            return true;
         }


        @Override
        protected void onProgressUpdate(Integer... values) {
            //insult user here
        }

        protected void onPostExecute(Boolean result) {
            dialog.dismiss();
            //dismiss splashscreen
            //start Phonegap
        }
    }

感谢您的阅读和帮助:)

2 个答案:

答案 0 :(得分:5)

我已经完成了这样的启动画面here。这会加载启动布局,并在加载应用程序时直接从系统触发(AndroidManifest.xml

<activity android:name=".SplashActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

屏幕上显示的内容在setContentView(R.layout.splash);中定义 - 此布局基本上是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/desktop_rhq"

        >

    <TextView android:layout_gravity="bottom"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:layout_marginBottom="10dp"
              android:text="@string/loading"
          />
</LinearLayout>

其中定义了背景图像和一些要显示的文本。说实话,在启动时我没有考虑方向更改 - 我想这会重新启动后台任务。

答案 1 :(得分:1)

<强> splash.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 

    android:src="@drawable/splash_image">

</ImageView>

<强> SplashActivity.class

public class SplashActivity extends Activity {

        protected boolean _active = true;
        protected int _splashTime = 1000; // time to display the splash screen in ms

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);

         // thread for displaying the SplashScreen
            Thread splashTread = new Thread() {
                @Override
                public void run() {
                    try {
                        int waited = 0;
                        while(_active && (waited < _splashTime)) {
                            sleep(100);
                            if(_active) {
                                waited += 100;
                            }
                        }
                    } catch(InterruptedException e) {

                    } finally {

                        startActivity(new Intent(SplashActivity.this, NextActivity.class));
                        finish();
                        //stop();
                    }
                }
            };
            splashTread.start();
        }
    }

对于方向更改:我在AndroidManifest.xml screenOrientation="portrait"

中设置
 <activity
            android:label="@string/app_name"
            android:screenOrientation="portrait" 
            android:theme="@android:style/Theme.NoTitleBar"
            android:name=".SplashActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>