本文实例总结了Android实现延迟的几种方法。分享给大家供大家参考,具体如下:
一、通过Thread
1
2
3
4
5
|
new Thread(){ public void run(){ sleep(***); } }.start(); |
通过ProgressDialog的使用来举例说明如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
public class A01Activity extends Activity { Button b; ProgressDialog pd; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); b=(Button)findViewById(R.id.b); b.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub final CharSequence dialogTitle=getString(R.string.dialogTitle); final CharSequence dialogBody=getString(R.string.dialogBody); pd=ProgressDialog.show(A01Activity. this , dialogTitle, dialogBody, true ); new Thread(){ public void run(){ try { sleep( 3000 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { pd.dismiss(); } } }.start(); } }); } } |
二、通过Timer
1
2
3
4
5
6
7
|
TimerTask task = new TimerTask(){ public void run(){ //execute the task } }; Timer timer = new Timer(); timer.schedule(task, delay); |
三、
1
2
3
4
5
|
new Handler().postDelayed( new Runnable(){ public void run() { //execute the task } }, delay); |
四、利用AlarmManager
希望本文所述对大家Android程序设计有所帮助。