从TimerTask类启动一个活动

时间:2015-02-08 10:24:07

标签: java android android-activity start-activity

如何在另一个班级中开始活动。它在日食中。它只是一段时间后开始主要活动。

        public class FirstShow extends ActionBarActivity {


        @SuppressLint("ShowToast")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.first_show);

            Timer tm;
            tm = new Timer();
            ttask task = new ttask();

            tm.schedule(task, 30000);



        }


    }


        class ttask extends TimerTask {

    @Override
        public void run() {
            // TODO Auto-generated method stub
            FirstShow.class.startActivity());

        }   

}

我想在几秒钟后开始另一项活动。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

Friend if you want normally want to start activity after few second than use my code:----->

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

                try {
                    sleep(3*1000);

                    Intent i=new Intent(getBaseContext(),ManuList.class);
                    startActivity(i);
                    finish();

                } catch (Exception e) {

                }
            }
        };

        thread.start();
    }

答案 1 :(得分:0)

     public class FirstShow extends ActionBarActivity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.first_show);

          new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Intent i=new Intent(FirstShow.this,secondactivity.class);
startActivity(i);

}
}, 3000);

        }
}

答案 2 :(得分:0)

package com.example.teststart;

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

public class FirstActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
           new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Intent i=new Intent(FirstActivity.this,secondactivity.class);
            startActivity(i);

            }
            }, 3000);

                    }

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

}

package com.example.teststart;

import android.app.Activity;
import android.os.Bundle;

public class secondactivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactvity);
}
}


 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.teststart.FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.example.teststart.secondactivity"
            android:label="@string/app_name" />
    </application>