20.11.2016 Views

ANDROID APP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

56 บทที่ 3 เธรด เซอร์วิส รีซีฟเวอร์ และการแจ้งเตือน<br />

ปุ่มกดที่ใช้ในชุดคำสั่งนี้จะมีหน้าที่เดียวกับการทำงานในชุดคำสั่งอันที่แล้ว ซึ่งจะยังแสดงปุ่มบน<br />

จอภาพ ในขณะที่ฟังก์ชั่น detectEdge() ยังคงทำงานอยู่เบื้องหลังเหมือนเดิม<br />

ชุดคำสั่งที่ 3.4 src/com/cookbook/runnable_activity/EdgeDetection.java<br />

package com.cookbook.runnable_activity;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.view.View;<br />

import android.widget.Button;<br />

import android.widget.TextView;<br />

public class EdgeDetection extends Activity implements Runnable {<br />

int numberOfTimesPressed=0;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

final TextView tv = (TextView) findViewById(R.id.text);<br />

//in-place function call causes main thread to hang:<br />

/* detectEdges(); */<br />

//instead, create background thread for time-consuming task<br />

Thread thread = new Thread(EdgeDetection.this);<br />

thread.start();<br />

Button startButton = (Button) findViewById(R.id.trigger);<br />

startButton.setOnClickListener(new View.OnClickListener() {<br />

public void onClick(View view){<br />

}<br />

});<br />

}<br />

tv.setText("Pressed button " + ++numberOfTimesPressed<br />

+ " times\nAnd computation loop at "<br />

+ "(" + xi + ", " + yi + ") pixels");<br />

@Override<br />

public void run() {<br />

detectEdges();<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!