0

我正在制作一个应用程序,我必须在其中显示一个欢迎页面仅 5 秒钟,然后重定向到另一个活动。但是,我没有得到我想要的,因为我先得到另一个活动,然后单击模拟器的后退按钮后得到 MainActivity,这是不正确的。那么有人可以帮助我吗?

我正在放置一些代码供您参考,特别是欢迎活动和清单代码:

 public class Welcome extends Activity   
 {  
    private Runnable runnable;  
    private static final String TAG="Welcome";  

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

        runnable=new Runnable() {  

            public void run() {  
                try  
                {  
                    Log.v(TAG,"Going to sleep...");  
                    Thread.sleep(3000);  
                    Log.v(TAG,"Going to wake up...");  

                }  
                catch(Exception e)  
                {  
                    e.printStackTrace();  
                }  
            }  
        };  
        try  
        {  
            Thread t=new Thread(null,runnable);  
            t.start();  
            Intent i=new Intent(this,TabSample.class);  
                startActivity(i);  
        }  
        catch(Exception e)  
        {  

        }  

    }  
  }  

AndroidManifest.xml

<activity
        android:name=".Welcome"
        android:label="Restaurant Application" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

主要的.xml

   <ImageView
       android:id="@+id/imageView1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_alignParentRight="true"
       android:src="@drawable/home" />
4

6 回答 6

0

像这样使用意图

Intent i=new Intent(Welcome.this,TabSample.class);  
startActivity(i);  
于 2012-09-26T10:56:28.777 回答
0

你应该试试这个。

    public class Welcome extends Activity   
     {  
        private Runnable runnable;  
        private static final String TAG="Welcome";  

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

            runnable=new Runnable() {  

                public void run() {  
                    try  
                    {  
                        Log.v(TAG,"Going to sleep...");  
                        Thread.sleep(3000);  
                        Log.v(TAG,"Going to wake up...");  

                       runOnUiThread(new Runnable() .
                         {

                         @Override
                         public void run() 
                         {
                            Intent i=new Intent(this,TabSample.class);  
                                           startActivity(i);  

                          }
            });


                    }  
                    catch(Exception e)  
                    {  
                        e.printStackTrace();  
                    }  
                }  
            };  
            try  
            {  
                Thread t=new Thread(null,runnable);  
                t.start();  

            }  
            catch(Exception e)  
            {  

            }  

        }  
      }  

因为如果您以与威胁并行的方式启动 Activity,那么它将毫无延迟地继续前进,并且线程将在后台运行。所以把你的startactivity代码放在runonuithread中。像上面一样。

它可能对你有用。

于 2012-09-26T10:57:30.633 回答
0

尝试这个

 Thread thread = new Thread() {
                public void run() {
                    try {


                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Intent intent = new Intent(Welcome.this, TabSample.class);
                    startActivity(intent);

                    finish();
                }
            };
            thread.start();
于 2012-09-26T10:58:33.987 回答
0

您可以使用 timertask 来显示一段时间的欢迎屏幕所以这就是你如何去做的。

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
public class Welcome extends Activity {
private long splashDelay = 5000; //5 seconds

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TimerTask task = new TimerTask()
    {

        @Override
        public void run() {
            finish();
            Intent mainIntent = new Intent().setClass(Welcome.this, TabSample.class);
            startActivity(mainIntent);
        }

    };

    Timer timer = new Timer();
    timer.schedule(task, splashDelay);
}
}
于 2012-09-26T10:58:36.050 回答
0

如果您只需要显示欢迎活动 5 秒钟,请修改您的主题。移除并插入

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

里面可运行。

于 2012-09-26T11:00:06.860 回答
0

您只能使用处理程序来做到这一点。代码将是这样的

Runnable GotoMenu;
Handler CallMenu = new Handler();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);

    GotoMenu = new Runnable(){
        public void run(){
            CallMenu.removeCallbacks(GotoMenu);
            Intent i = new Intent(getApplicationContext(), TabSample.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            finish();
        }
    };
    CallMenu.postDelayed(GotoMenu, 2000);

}
于 2012-09-26T11:08:03.240 回答