how to display another activity
How to display another activity? I want to display one activity which has one Image View splash displaying an image. I want to display that just for 5 seconds then move on to another activity.splash is displaying in emulator but another activity menu is not displaying.
Here is my code:
com.basic.android;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
impo开发者_JAVA技巧rt android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class androidbasics extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
public void toCallActivity() {
TimerTask startNewActivity;
final Handler handler = new Handler();
final Timer timer = new Timer();
startNewActivity = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
handler.post(new Runnable() {
public void run() {
try {
timer.cancel();
startActivity(new Intent(androidbasics.this,menu.class));
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(startNewActivity, 0,5000);
}
}
You didn't call toCallActivity().So your new Activity is not coming in the front.Write like this.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
toCallActivity();
}
try this ....
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
BackgroundTask b = new BackgroundTask();
b.execute("Main");
}
/** Called when the activity is first created. */
class BackgroundTask extends AsyncTask<String , Void, Void>
{
@Override
protected void onPreExecute()
{
setContentView(R.layout.splash);
}
@Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
int pause=5000;
int interval =1000;
int counter=0;
while(counter<pause)
{
try
{
Thread.sleep(interval);
counter+=interval;
}
catch(Exception e)
{
System.out.println(e);
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
startActivity( new Intent(androidbasics.this,menu.class));
androidbasics.this.finish();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
}
catch(InterruptedException e)
{
} finally {
finish();
startActivity(new Intent(firstactivity.class,secondActivity.class));
stop();
}
}
};
splashTread.start();
}
精彩评论