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