⑴ android搴旂敤鎯沖疄鐜拌窡鏂版氮寰鍗氬樊涓嶅氱殑嬈㈣繋鐣岄潰錛岄氳繃postDelayed鏂規硶瀹炵幇榪5縐掕煩杞錛
榪欐槸涓縐嶅彲浠ュ壋寤哄氱嚎紼嬫秷鎮鐨勫嚱鏁
浣跨敤鏂規硶錛
1錛岄栧厛鍒涘緩涓涓狧andler瀵硅薄
Handler handler=new Handler();
2錛岀劧鍚庡壋寤轟竴涓猂unnable瀵硅薄
Runnable runnable=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//瑕佸仛鐨勪簨鎯咃紝榪欓噷鍐嶆¤皟鐢ㄦRunnable瀵硅薄錛屼互瀹炵幇姣忎袱縐掑疄鐜頒竴嬈$殑瀹氭椂鍣ㄦ搷浣
handler.postDelayed(this, 2000);
}
};
3錛屼嬌鐢≒ostDelayed鏂規硶錛屼袱縐掑悗璋冪敤姝Runnable瀵硅薄
handler.postDelayed(runnable, 2000);
瀹為檯涓婁篃灝卞疄鐜頒簡涓涓2s鐨勪竴涓瀹氭椂鍣
4錛屽傛灉鎯寵佸叧闂姝ゅ畾鏃跺櫒錛屽彲浠ヨ繖鏍鋒搷浣
handler.removeCallbacks(runnable);
褰撶劧錛屼綘涔熷彲浠ュ仛涓涓闂歸挓鎻愰啋寤舵椂鐨勫嚱鏁拌瘯璇曪紝姣斿傦紝鍏堢敤MediaPlayer鎾鏀鵑椆閽熷0闊籌紝
濡傛灉涓嶆兂璧鳳紝琚鍋滄㈡挱鏀句箣鍚庯紝涓嬫″氨5鍒嗛挓鍚庡啀鎾鏀撅紝鍐嶈鍋滄㈢殑璇濓紝涓嬫″氨4鍒嗛挓鍚庢挱鏀撅紝
鈥︹︹︹︹︹
鍙瑕佹洿鏀瑰歡鏃剁殑鏃墮棿灝卞彲浠ュ疄鐜頒簡錛岀敤涓涓猻tatic瀵硅薄鐨勮瘽浼氭瘮杈冨規槗鎿嶄綔銆
鏄鍙浠ュ紓姝ユ晥鏋滐紝浣哛unnable鐨勬墽琛屾槸鍦℉andler瀵硅薄鎵鍦ㄧ殑綰跨▼
濡傛灉鍏舵墍鍦ㄧ殑綰跨▼鏄疷I綰跨▼鐨勮瘽錛孯unnable涓榪樻槸涓嶈兘鎵ц岃楁椂鎿嶄綔錛屼笉鐒朵細ANR
鍓嶅嚑澶╂垜浠鑷宸辯殑璁懼囧緢鍗★紝鍗″埌璺寵漿鐣岄潰閮介渶瑕佷笉鍒1縐掔殑鏃墮棿錛屾垜灝辨妸璺寵漿鐨勫姩浣滄斁鍦≧unnable閲岃竟錛屽栬竟鍔犱笂寮瑰嚭榪涘害鎻愮ず妗
娉錛氫婦渚嬭存槑
public class XXX extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.loading); // 鏄劇ず絎1灞
Handler handler = new Handler();
handler.postDelayed(new splashhandler(), 2000); // 寤惰繜2縐掞紝鍐嶈繍琛宻plashhandler鐨剅un()
}
class splashhandler implements Runnable
{
public void run()
{
startActivity(new Intent(getApplication(), SecondActivity.class)); // 鏄劇ず絎2灞
XXX.this.finish(); // 緇撴潫絎1灞
}
}
}