AsyncTask 运行时的 Android 闪屏

标签 android splash-screen

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

在应用加载 index.html 之前,有一些 native 代码在工作。详细地说,它在 AsyncTask 中,它在 SD 卡上(如果有的话)来回敲打。

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

这是来自源代码的 fragment 。直截了当。 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
        }
    }

感谢阅读和帮助:)

最佳答案

我做过这样的启动画面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>

它定义了背景图像和一些要显示的文本。老实说,在启动画面运行时,我没有考虑太多关于方向变化的问题——我猜这会重新启动后台任务。

关于AsyncTask 运行时的 Android 闪屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654285/

相关文章:

java - 如何从初始页面过渡到主要 Activity ?

ios - 加载应用程序时 Cordova iOS 启动图像更改

WPF应用程序窗口出现在SplashScreen的顶部

android - 我们如何区分 Android M 的运行时权限中的从不询问和停止询问?

android - 如何给 Canvas 绘制的元素添加 Material 风格的阴影?

android - 如何反编译kotlin android apk?

从网络驱动器启动的 c# .net4 应用程序需要很长时间才能加载

android - Flutter,Android - 是否可以更改初始(启动)屏幕上的状态栏颜色?

java - android 上的 noob SQLite 光标错误

java - Android:如何在 OnItemClickListener() 中调用 getActivity()?