如何在显示初始屏幕时在后台设置一些工作

时间:2016-07-16 06:53:12

标签: android backgroundworker

我想在显示启动画面时做一些像填充哈希映射的工作。但我不知道我是怎么做的

我认为一种方法是创建新线程?

我的启动画面代码

public class SplashActivity extends AppCompatActivity {

    long Delay = 2000;
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashactivity);
        StartAnimations();
        Timer RunSplash = new Timer();

        TimerTask ShowSplash = new TimerTask() {
            @Override
            public void run() {
                finish();
                Intent myIntent = new Intent(SplashActivity.this,LoginEnquiryTab.class);
                overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_left);
                startActivity(myIntent);
            }
        };

        RunSplash.schedule(ShowSplash,Delay);
    }

1 个答案:

答案 0 :(得分:0)

不要为启动画面创建单独的活动。 您的启动画面应仅在启动活动中。

<强> styles.xml

<style name="AppTheme.Splash" parent="AppTheme">
    <item name="android:windowBackground">@mipmap/ic_launcher</item>
</style>

<强>的AndroidManifest.xml

<activity android:name="com.architjn.example.ui.activity.MainActivity"
    android:theme="@style/AppTheme.Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

<强> MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    //do your work here while splash shows..
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Google也使用上述方式。