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.

การสื่อสารข้อมูลระหว่างโปรเซสภายนอกของแอนดรอยด์<br />

291<br />

android:layout_height="wrap_content" android:text="result"<br />

android:textSize="36dp" android:id="@+id/result"><br />

<br />

ในชุดคำสั่งที่ 11.10 จะแสดงแอททริบิวต์ชื่อ android:process=”.remoteService”<br />

ซึ่งคำสั่งนี้จะสั่งให้ระบบทำการสร้างโปรเซสชื่อ remoteService เพื่อใช้ในการรันแอคทิวิตี้ที่ 2<br />

ชุดคำสั่งที่ 11.10 AndroidManifest.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

แอคทิวิตี้ตัวที่ 2 ที่แสดงในชุดคำสั่งที่ 11.11 นั้นจะต้องใช้คำสั่ง bindService() เพื่ออ่าน<br />

ข้อมูลจากฟังก์ชั่น factorial() โดยใช้ rpcService และคำสั่ง bindService() นี้จะต้องมี<br />

อินสแตนซ์สำหรับเชื่อมต่อเพื่อใช้เป็นอินเตอร์เฟซสำหรับตรวจสอบสถานะของแอพ<br />

คลาส myServiceConnection และคลาส IAdditionService จะสร้างอินสแตนซ์ไว้ใน<br />

แอคทิวิตี้ rpc ซึ่งคำสั่ง myServiceConnection จะคอยตรวจจับอีเวนต์ onServiceConnected<br />

และ onServiceDisconnected โดยที่ onServiceConnected จะส่งค่าของอินสแตนซ์ IBinder<br />

ไปยังอินสแตนซ์ IAdditionService และ onServiceDisconnected จะกำหนดค่าของ IAdditionService<br />

เป็น null<br />

ภายในแอคทิวิตี้ rpc มีการสร้างเมธอดจำนวน 2 ตัว คือ initService() ที่ใช้ในการเริ่มการ<br />

ทำงานของ myServiceConnection ซึ่งมันจะสร้างอินเท็นต์เพื่อกำหนดชื่อแพ็คเกจและชื่อคลาสเพื่อ<br />

ส่งค่าไปยัง bindService ด้วยอินสแตนซ์ myServiceConnection และกำหนดค่า BIND_AUTO_<br />

CREATE

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

Saved successfully!

Ooh no, something went wrong!