Monday 4 June 2012

When to use Intent ? For What ?

Category: Android Development

เราใช้ Intent เมื่อเราต้องการสั่ง start Activity หรือ Service และเรายังใช้ Intent สำหรับรับ/ส่ง ข่าวสารจาก System ยกตัวอย่างเช่น เราสามารถรับ Notification เกี่ยวกับสถานะของ Battery ที่มีการเปลี่ยนแปลง โดยการ register เพื่อรับ intent (widely published intent) ถ้าหาก Activity ไหนก็ตาม มีการ register เพื่อรับ Intent ไว้ใน Manifest File (ยกตัวอย่างเช่น com.yahoo.www.ShowMe) นั่นหมายความว่า Application ใดก็ตามที่อยู่บนโทรศัพท์สามารถสั่ง start Activity ของเราได้ (ถ้าเราประกาศให้ Activity เป็นแบบ Public) โดยการใช้คำสั่ง startActivity(new Intent("com.yahoo.www.ShowMe"));

แหล่งที่มา : หนังสือ Creating Android Applications ของ Chris Haseman

No comments:

Post a Comment