下午写了一篇,结果格式错了,很难看。我再重新编辑一下。
现在很多软件都有一运行就有一个欢迎页面,然后过几秒之后就会转到主页面。我也尝试了一下。其实算不上难,只需要一个XML文件和一个Java文件就可以。将其通过setcontentview连接起来,再导入MP3,通过mediaplayer播放就可以。
项目目录如下:
歌曲我选择的是韩国歌手Aliee的成名曲,给你看。个人觉得比较好,比较动感。嘿嘿。
open.java的代码如下:
package com.example.happyidiom;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
public class Open extends Activity{
MediaPlayer oursong;
private final long SPLASH_LENGTH = 35000;
Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open);
oursong=MediaPlayer.create(Open.this, R.raw.aliee);
oursong.start();
handler.postDelayed(new Runnable() { //使用handler的postDelayed实现延时跳转
public void run() {
Intent intent = new Intent(Open.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_LENGTH);//2秒后跳转至应用主界面MainActivity
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
oursong.stop();
finish();
}
}
这是其中的一种方法,还有另一种方法,但是我不是很熟练。
如下:
package cn.edu.bztc.pop;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splah extends Activity{
MediaPlayer oursong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
oursong=MediaPlayer.create(Splah.this, R.raw.aliee);
oursong.start();
Thread timer=new Thread(){
public void run(){
try{
sleep(35000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent open=new Intent("cn.edu.bztc.pop.MENU");
startActivity(open);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
oursong.stop();
finish();
}
}
个人觉得第一种方法比较简便。方便使用。第二种因为一直显示有错误,我也没有调试出来。
下面是open.xml的页面:
然后再在manifest中加入如下代码:
<activity
android:name="com.example.happyidiom.Open"
android:label="open"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
就可以运行程序了。
就是在启动程序时MP3播放,open页面打开。
之后就跳转到主页面。