1.) Create a new project by File-> New -> Android Project name it HandlerThreadActivity.
2.) Write following into main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
| <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <Button android:id= "@+id/Button01" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/start_thread" ></Button> </LinearLayout> |
3.) Run for output.
Steps:
1.) Create a project named HandlerThreadActivity and set the information as stated in the image.
Build Target: Android 4.2
Application Name: HandlerThreadActivity
Package Name: com. HandlerThreadActivity
Activity Name: HandlerThreadActivity
Min SDK Version: 4.2
Application Name: HandlerThreadActivity
Package Name: com. HandlerThreadActivity
Activity Name: HandlerThreadActivity
Min SDK Version: 4.2
2.) Open HandlerThreadActivity.java file and write following code there:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| package com.handlerthreadactivity; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class HandlerThreadActivity extends Activity{ private Button start; private ProgressDialog progressDialog; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button) findViewById(R.id.Button01); start.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub fetchData(); } }); } protected void fetchData() { // TODO Auto-generated method stub progressDialog = ProgressDialog.show( this , "" , "Loading..." ); new Thread() { public void run() { try { Thread.sleep( 800 ); } catch (InterruptedException e) { } messageHandler.sendEmptyMessage( 0 ); } }.start(); } private Handler messageHandler = new Handler() { public void handleMessage(Message msg) { super .handleMessage(msg); progressDialog.dismiss(); } }; } |
4.) Compile and build the project.
No comments:
Post a Comment