7ef9709749f9909b2b3d7be5bf2ae562f7c5b0a1
[UdpRespond.git] / src / com / example / udpresponder / UdpRespondActivity.java
1 package com.example.udpresponder;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.os.Handler;
6 import android.os.Message;
7 import android.os.SystemClock;
8 import android.widget.TextView;
9 import android.widget.Button;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12
13 public class UdpRespondActivity extends Activity {
14
15         private Handler mHandler = new Handler() {
16                 public void handleMessage(Message msg) {
17                         Bundle bdl = msg.getData();
18                         updateUI(bdl.getString("ln"));
19                 }
20         };
21
22         private TextView tv;
23
24         private OnClickListener mStartListener = new OnClickListener() {
25                 public void onClick(View v) {
26                         tv.append("Start pressed\n");
27                 }
28         };
29
30         private OnClickListener mStopListener = new OnClickListener() {
31                 public void onClick(View v) {
32                         tv.append("Stop pressed\n");
33                         finish();
34                 }
35         };
36
37         @Override
38         public void onCreate(Bundle savedInstanceState) {
39                 super.onCreate(savedInstanceState);
40                 setContentView(R.layout.main);
41                 tv = (TextView)findViewById(R.id.tv);
42                 tv.append(" Starting\n");
43                 Button btn_start = (Button)findViewById(R.id.btn_start);
44                 btn_start.setOnClickListener(mStartListener);
45                 Button btn_stop = (Button)findViewById(R.id.btn_stop);
46                 btn_stop.setOnClickListener(mStopListener);
47                 startBgThread();
48         }
49
50         private void updateUI(String str) {
51                 tv.append(str);
52         }
53
54         protected void startBgThread () {
55                 Thread t = new Thread() {
56                         public void run() {
57                                 while (true) {
58                                         SystemClock.sleep(3000);
59                                         Bundle bdl = new Bundle();
60                                         bdl.putString("ln", "this is msg\n");
61                                         Message msg = Message.obtain(mHandler);
62                                         msg.setData(bdl);
63                                         msg.sendToTarget();
64                                 }
65                         }
66                 };
67                 t.start();
68         }
69 }