在我的adnroid项目中,我添加了一个启动画面,我显示它5秒钟,我想启动我的MainMenu活动,但是启动画面没有出现在屏幕上,它等待我9秒,然后直接进入我的MainMenu布局,如果我丢弃那部分代码它显示我的Splash我找不到我的代码有什么问题所以这里是我的Splash类
public class Splash extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread timer = new Thread(){
@Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
Intent startIntent;
startIntent = new Intent("android.intent.action.MAINMENU");
startActivity(startIntent);
}
}
};
timer.run();
}
}
这是我的MainMenu类
public class MainMenu extends Activity {
private static ArrayList<ArrayList> result = new ArrayList<ArrayList>();
TextView tx;
String temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
tx = (TextView) findViewById(R.id.tvView);
temp = "";
try {
result = MethodInfoGetter.methodRequest("BaskanOzgecmisGetir", "", "");
Log.i("MainMenu","Result gelis vakti");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*if(result.isEmpty())
{
tx.setText("Result is emty");
}
else
{
for(int i = 0 ; i < result.size() ; i++)
{
for(int j = 0 ; j < result.get(i).size() ; j++ )
{
temp += result.get(i).get(j) + " \n";
}
}
}
tx.setText(temp);*/
}
@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, menu);
return true;
}
}
如果你们这里需要的是我的android清单文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >"
<activity
android:name="com.BLKBelediye.balikesirbelediye.MainMenu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINMENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.BLKBelediye.balikesirbelediye.Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:1)
您需要使用方法start()而不是run()来启动线程,以便线程以异步方式运行。
用timer.start()
替换timer.run()timer.run()调用使您当前的UI线程休眠5秒钟。
答案 1 :(得分:0)
使用CountDownTimer
public class MainActivity extends Activity
{
private Timer_Countdown timer_Countdown = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer_Countdown = new Timer_Countdown(5000, 1000);
timer_Countdown.start();
}
class Timer_Countdown extends CountDownTimer
{
public Timer_Countdown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
timer_Countdown.cancel();
Intent startIntent;
startIntent = new Intent("android.intent.action.MAINMENU");
startActivity(startIntent);
}
@Override
public void onTick(long millisUntilFinished) {
}
}
}