-public class UdpRespondActivity extends Activity
-{
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
+public class UdpRespondActivity extends Activity {
+
+ private Handler mHandler = new Handler() {
+ public void handleMessage(Message msg) {
+ Bundle bdl = msg.getData();
+ updateUI(bdl.getString("ln"));
+ }
+ };
+
+ private TextView tv;
+
+ private OnClickListener mStartListener = new OnClickListener() {
+ public void onClick(View v) {
+ tv.append("Start pressed\n");
+ }
+ };
+
+ private OnClickListener mStopListener = new OnClickListener() {
+ public void onClick(View v) {
+ tv.append("Stop pressed\n");
+ finish();
+ }
+ };
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+ tv = (TextView)findViewById(R.id.tv);
+ tv.append(" Starting\n");
+ Button btn_start = (Button)findViewById(R.id.btn_start);
+ btn_start.setOnClickListener(mStartListener);
+ Button btn_stop = (Button)findViewById(R.id.btn_stop);
+ btn_stop.setOnClickListener(mStopListener);
+ startBgThread();
+ }
+
+ private void updateUI(String str) {
+ tv.append(str);
+ }
+
+ protected void startBgThread () {
+ Thread t = new Thread() {
+ public void run() {
+ while (true) {
+ SystemClock.sleep(3000);
+ Bundle bdl = new Bundle();
+ bdl.putString("ln", "this is msg\n");
+ Message msg = Message.obtain(mHandler);
+ msg.setData(bdl);
+ msg.sendToTarget();
+ }
+ }
+ };
+ t.start();
+ }