5

你能告诉我如何在android中使用Timer显示启动画面。我可以使用线程显示,但是线程是不错的选择,你能告诉我处理这个问题的最佳方法吗?像这样使用线程

包 com.example.splash_test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
    protected int _splashTime = 5000; 

    private Thread splashTread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         splashTread = new Thread() {
             @Override
             public void run() {
                 try {                       
                     synchronized(this){
                             wait(_splashTime);
                     }

                 } catch(InterruptedException e) {} 
                 finally {
                     finish();

                     Intent i = new Intent();
                     i.setClass(MainActivity.this, SecondActivity.class);
                             startActivity(i);

                     //stop();
                 }
             }
         };

         splashTread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
4

6 回答 6

5

简而言之,此代码段对于设计计时器非常有帮助。尝试这个...

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
    }
}, 5000);

在这里,我MainActivity在 5 秒后开始。只需将此代码放入onCreate()您的启动活动中。

于 2015-04-04T18:23:17.883 回答
1
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class SplashScreen extends Activity {

protected int _splashTime = 2000;

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

    int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            startActivity(new Intent(SplashScreen.this,
                    SecondActivity.class));
            finish();
        }
    }, secondsDelayed * 1000);
}

}
于 2013-09-18T10:11:52.590 回答
1
out_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="50dp"
            android:gravity="center"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/txtProgress"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:progress="50"
                android:progressTint="@android:color/black" />

            <TextView
                android:id="@+id/txtProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Progress"
                android:textColor="@android:color/black" />
        </LinearLayout>  

Java
于 2019-06-04T05:59:38.123 回答
1
new Handler().postDelayed(new Runnable() {

/*
 * Showing splash screen with a timer. This will be useful when you
 * want to show case your app logo / company
 */

    @Override
    public void run() {
        // This method will be executed once the timer is over
        // Start your app main activity

            Intent i = new Intent(SplashScreen.this, YourActivity.class);
            startActivity(i);

        // close this activity
        finish();
    }
}, 1000);
于 2019-06-04T06:12:01.320 回答
0

你也可以在你的 onCreate 中插入:

SystemClock.sleep(TimeUnit.SECONDS.toMillis(3));

ps 检查这个 SplashScreen 的实现链接

于 2017-03-11T23:20:14.027 回答
0

科特林方式

Timer().schedule(object : TimerTask() {
        override fun run() {
            startActivity(Intent(applicationContext, MainActivity::class.java))
        }
    }, 3000)
于 2021-05-05T08:12:45.110 回答