SlideShare a Scribd company logo
Service
MultiUni
Trần Vũ Tất Bình
Tổng quan
• Là một trong 4 loại thành phần của một ứng
dụng.
• Service chạy nền và không tương tác trực tiếp
với người dùng.
• Tham khảo:
http://developer.android.com/reference/android/a
pp/Service.html
Lifecycle – startService()
onCreate()
onStart()
onDestroy()
Service
đang chạy
Service được start
lần đầu
Service lại được yêu
cầu start trong khi đã
chạy rồi
Bên ngoài gọi
stopService() hoặc
service tự stop
Lifecycle – startService()
• Khi có một context nào đó gọi startService()
để start service mong muốn. Nếu service đó
chưa được tạo thì sẽ gọi onCreate() rồi gọi tiếp
onStart() và khi đó service chạy nền bên dưới.
• Nếu sau đó lại có một context muốn start
service này mà service đã đang chạy, chỉ có
phương thức onStart() của service được gọi.
• Dù service có được gọi start bao nhiêu lần thì
cũng chỉ có 1 instance của service và chỉ cần
gọi stopService() một lần để kết thúc service.
Lifecycle – bindService()
onCreate() onBind()
Service
đang chạy
Activity gọi
bindService() để
kết nối tới
service
Bên ngoài gọi
unbindService()
hoặc kết nối bị
ngắt
onUnbind()
Service không còn
kết nối với client
nào nữa và cũng ko
có context nào start
service này
onDestroy()
Service còn chạy,
client muốn kết nối
với service
onRebind()
Lần trước
unBind() với
client này
trả về true
Lifecycle – bindService()
• Thông thường, lifecycle của service khi có
client kết nối từ đầu như sau:
– Cũng bắt đầu bằng onCreate() rồi đến onBind() và
service chạy background.
– Khi không còn client kết nối tới thì service gọi
onUnbind() rồi onDestroy().
Lifecycle
• Có một số trường hợp không thông thường, ví
dụ như:
– Có một context khởi động (start) một service, sau
đó có một số client kết nối (bind) tới service
– Có nhiều client cùng lúc kết nối (bind) tới service
– Một activity vừa gọi startService() vừa gọi
bindService()
…
Các bạn có thể tự làm ví dụ để thử các trường hợp
này.
Xem demo LocalService
• ApiDemos  src 
com.example.android.apis.app
 LocalService
 LocalServiceBinding
LocalServiceController
Sử dụng service
– Dùng trong các ứng dụng nghe nhạc.
– Dùng để xử lý các thao tác mất thời gian và không
nhất thiết phải hiển thị lên activity (download,
upload…)
– Đôi khi cần một ứng dụng vận hành liên tục để xử
lý những việc mong muốn mà không làm phiền
người dùng  service
– Làm những thao tác tính toán, xử lý đều đặn nào
đó và kết quả khi nào người dùng cần thì mới xem
…

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Hướng Dẫn Lập Trình Android: 6. About Service

  • 2. Tổng quan • Là một trong 4 loại thành phần của một ứng dụng. • Service chạy nền và không tương tác trực tiếp với người dùng. • Tham khảo: http://developer.android.com/reference/android/a pp/Service.html
  • 3. Lifecycle – startService() onCreate() onStart() onDestroy() Service đang chạy Service được start lần đầu Service lại được yêu cầu start trong khi đã chạy rồi Bên ngoài gọi stopService() hoặc service tự stop
  • 4. Lifecycle – startService() • Khi có một context nào đó gọi startService() để start service mong muốn. Nếu service đó chưa được tạo thì sẽ gọi onCreate() rồi gọi tiếp onStart() và khi đó service chạy nền bên dưới. • Nếu sau đó lại có một context muốn start service này mà service đã đang chạy, chỉ có phương thức onStart() của service được gọi. • Dù service có được gọi start bao nhiêu lần thì cũng chỉ có 1 instance của service và chỉ cần gọi stopService() một lần để kết thúc service.
  • 5. Lifecycle – bindService() onCreate() onBind() Service đang chạy Activity gọi bindService() để kết nối tới service Bên ngoài gọi unbindService() hoặc kết nối bị ngắt onUnbind() Service không còn kết nối với client nào nữa và cũng ko có context nào start service này onDestroy() Service còn chạy, client muốn kết nối với service onRebind() Lần trước unBind() với client này trả về true
  • 6. Lifecycle – bindService() • Thông thường, lifecycle của service khi có client kết nối từ đầu như sau: – Cũng bắt đầu bằng onCreate() rồi đến onBind() và service chạy background. – Khi không còn client kết nối tới thì service gọi onUnbind() rồi onDestroy().
  • 7. Lifecycle • Có một số trường hợp không thông thường, ví dụ như: – Có một context khởi động (start) một service, sau đó có một số client kết nối (bind) tới service – Có nhiều client cùng lúc kết nối (bind) tới service – Một activity vừa gọi startService() vừa gọi bindService() … Các bạn có thể tự làm ví dụ để thử các trường hợp này.
  • 8. Xem demo LocalService • ApiDemos  src  com.example.android.apis.app  LocalService  LocalServiceBinding LocalServiceController
  • 9. Sử dụng service – Dùng trong các ứng dụng nghe nhạc. – Dùng để xử lý các thao tác mất thời gian và không nhất thiết phải hiển thị lên activity (download, upload…) – Đôi khi cần một ứng dụng vận hành liên tục để xử lý những việc mong muốn mà không làm phiền người dùng  service – Làm những thao tác tính toán, xử lý đều đặn nào đó và kết quả khi nào người dùng cần thì mới xem …