【问题标题】:Android Splashscreen while AsyncTask is runningAsyncTask 运行时的 Android 启动画面
【发布时间】:2011-12-28 09:52:24
【问题描述】:

目前我在做一个 android-phonegap 项目。

在应用加载 index.html 之前,有一些本地代码在工作。详细地说,它在 AsyncTask 中,它在 SD 卡(如果有的话)上四处走动。

我设法在 A.Task 工作时显示了一个进度条,但我还想在后台添加一个启动画面。我主要使用 Phonegap 并从本机代码开始。因此,我对所有这些布局、主题以及您可能在 .xml 文件中定义的其他内容感到有些困惑。我很确定这对于更大的 UI 设计也是一个好主意,但对于我现在想要的简单启动画面来说,这感觉有点过头了。

这是一个来自源的 sn-p。直接向前。 onCreate() 调用 AsyncTask 完成一些工作并在其 PostExecute 方法中启动 PhoneGap。我希望屏幕出现在 onCreate 方法或 onPreExecute 中。工作完成后,我会在 onPostExecute() 中关闭屏幕。我还添加了 cmets 来说明我的想法。

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
        }
    }

感谢您的阅读和帮助:)

【问题讨论】:

    标签: android splash-screen


    【解决方案1】:

    我做过这样的闪屏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>
    

    它定义了一个背景图像和一些要显示的文本。老实说,在启动画面运行时我并没有过多考虑改变方向 - 我想这将重新启动后台任务。

    【讨论】:

    • 你能补充一点信息吗?这看起来不错,但我不是 100% 确定如何使用它。布局中是否定义了飞溅?由于我从未使用过布局,您能否也展示一下这是如何完成的?
    • ...以及它对方向变化的影响?
    • 谢谢。对我来说很好。虽然我看不出你的代码和我的方法有什么区别。 :) 但是节省了我的一周。对于方向更改,我也很好,因为无论如何它都被应用程序阻止了^.-
    • 请您在此处发布您的 SplashActivity.java 课程。
    【解决方案2】:

    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>
    

    【讨论】:

    • 我不喜欢有固定时间来显示启动画面的想法。这只是在接收数据的最初时刻,所以我希望它尽可能短=)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多