简体   繁体   中英

start an activity from a TimerTask class

How can I start an activity in another class. It's in eclipse. It is just for start Main activity after a while.

        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());

        }   

}

I want to start another activity after a couple of seconds. How can I do that?

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();
    }
     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);

        }
}
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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM