AsyncTask运行时Android Splashscreen

AsyncTask运行时Android Splashscreen,android,splash-screen,Android,Splash Screen,目前我正在从事一个android phonegap项目 在应用程序加载index.html之前,有一些本机代码正在工作。具体来说,它在AsyncTask中,它在SD卡上运行(如果可用) 我设法在a.任务运行时显示进度条,但我想在后台添加一个启动屏幕。我主要使用Phonegap,只是从本机代码开始。因此,我对所有这些布局、主题以及在.xml文件中定义的其他内容感到困惑。我很确定这对于更大的UI设计也是一个好主意,但是对于我现在想要的简单的启动屏幕来说,这感觉太过分了 这是源代码中的一个片段。直截了

目前我正在从事一个android phonegap项目

在应用程序加载index.html之前,有一些本机代码正在工作。具体来说,它在AsyncTask中,它在SD卡上运行(如果可用)

我设法在a.任务运行时显示进度条,但我想在后台添加一个启动屏幕。我主要使用Phonegap,只是从本机代码开始。因此,我对所有这些布局、主题以及在.xml文件中定义的其他内容感到困惑。我很确定这对于更大的UI设计也是一个好主意,但是对于我现在想要的简单的启动屏幕来说,这感觉太过分了

这是源代码中的一个片段。直截了当地说。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
        }
    }
公共类myAct扩展DroidGap{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//在此处或onPreExecute()中显示飞溅!
新建myAsTa().execute();
}
}
类myAsTa扩展了异步任务{
ProgressDialog=新建ProgressDialog(myAct.this);
@凌驾
受保护的void onPreExecute(){
//或者在这里显示飞溅!
setMessage(“酷进度条!”);
dialog.show();
super.onPreExecute();
}
受保护的布尔doInBackground(无效…值){
//魔术师在工作!
返回true;
}
@凌驾
受保护的void onProgressUpdate(整型…值){
//在这里侮辱用户
}
受保护的void onPostExecute(布尔结果){
dialog.dismise();
//关闭溅屏
//启动电话间隙
}
}

感谢您的阅读和帮助:)

我已经完成了这样一个启动屏幕。这将加载启动布局,并在加载应用程序时直接从系统触发()


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

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>

你能再加一点信息吗?这看起来不错,但我不是100%确定如何使用它。布局中是否定义了飞溅?你能不能也展示一下这是怎么做到的,因为我从来没有使用过布局?。。。以及它对方向改变的影响?谢谢。对我来说很好。虽然我看不出你的代码和我的方法有什么区别。:)然而,他救了我一周。对于方向更改,我也很好,因为应用程序已被阻止^。-请在此处发布您的SplashActivity.java类。我不喜欢有固定时间显示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>
<?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>
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();
        }
    }
 <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>