SlideShare a Scribd company logo
1 of 42
Service@Android
Charlie Tsai
Agenda
• Basic Knowledge
• Intent Service
• Start/Stop Service
• Bound Service
• Isolated Service
• Exported Service
• Notification
Basic
UIThread and
WorkerThread
• UIThread
Only UIThread can control the UIWidgets
Why?
• WorkerThread
Use WorkerThread to avoid blocking UI
Some built-in tools help to use WorkerThread
ex: Looper & Handler, AsyncTask.
What is Service?
• One of application components of Android
Need to be declared in AndroidManefest.xml
• Usually used to do long-tern background work
• Service can interactive with other component
(exported = true)
• Service can (but not need to) run in another process
• Service is running in UI Thread
Service in AndroidManifest
• android:name=“[package/service]”
• android:enabled=“[true|false]”
• android:exported=“[true|false]”
• android:isolatedProcess=“[true|false]”
• android:process=“[name/of/process]”
Intent Service
Intent Service
• It is implemented to do something in non-UI
thread.
• we only need to implement the WorkerThread
• It only execute one at time (queueing)
• It used HandlerThread
• It will stop when completed
Start/Stop Service
• Can be started/stopped several times, but only
one instance. (implicit singleton)
• No start/stop counting
• Service can stop itself. (call Service.stopSelf())
• If you need to start service for specified event,
use Broadcast Receiver.
int onStartCommand
• The return value define the behavior of service
• return START_STICKY
• return START_NOT_STICKY
• return START_REDELIVER_INTENT
• flag: START_FLAG_REDELIVERY,
START_FLAG_RETRY
Start and Stop Service
• Context.startService(Intent)
• Context.stopService(Intent)
• Service.stopSelf()
Bound Service
Bind Service
• A service can be bind several times.
And service will maintain a bindCount
• When someone bind service, bindCount + 1
• When someone unbind service, bindCount - 1
• Service destroy itself when bindCount == 0
Context.bindService()
• bindService(Intent, ServiceConnection, int flags);
• flags:
BIND_AUTO_CREATE
BIND_NOT_FOREGROUND
BIND_ABOVE_CLIENT
BIND_ALLOW_OOM_MANAGEMENT
BIND_WAIVE_PRIORITY
Hyper Service
(start/stop + bind)
Hyper Service
• Service create:
startService() or bindService() when no service
created
• Service destroy:
if has been started: stopService()(or stopSelf())
&&
if has been bound: bindCount == 0
Isolated Service
Isolated Service
• android:isolatedProcess=“true”
• android:process=“:[process_name]”
(Note: the “:” is needed)
• Even you give two isolated processes the same
process name, they will NOT run in same process.
Thus, you get at least 3 process: app, service1, and
service2
The process names of service1 and service2 are same
IPC issues
• # IPC = Inter Process Communication
• Since the service is run in another process, we
can not call its function directly
• Solution:
Messenger:
Simple, but the calling order is not guarantee
AIDL:
Hard to implement, guarantee the calling order
Messenger
• Create a Handler to receive commands
• Create a Messenger and use Handler
• return Messenger.getBinder() in onBind()
• disadvantage:
The arguments cannot be customized
The messenger system is asynchronized
# function may not run immediately when called
AIDL
• the function call is synchronized
# function call is blocked until return
• This is too complexity so we don’t discuss here
• Please check official document:
http://developer.android.com/intl/zh-
tw/guide/components/aidl.html
Exported Service
Exported Service
• Exported Service make you use other
application’s service
• The exported service is run in the process of its
application, NOT in the process of caller
• If used as bound service, the binder of it should
support IPC
(Thus, it usually used with AIDL.)
Notification
Show Notification
• Notification is part of Service
• Use NotificationCompat.Builder
• startForeground(int notificationId, Notification)
# the notificationId must NOT be 0
• startForeground with same id will replace the
previous notification which has same id.
Conclusion
• IntentService
• Start / Stop Service
• Bound Service (bind/unbind)
• Isolated Service
• Exported Service
• Notification
Thanks!

More Related Content

What's hot

Inter Process Communication (IPC) in Android
Inter Process Communication (IPC) in AndroidInter Process Communication (IPC) in Android
Inter Process Communication (IPC) in AndroidMalwinder Singh
 
Stage migration, exchange and autodiscover infrastructure part 1#2 part 35#36
Stage migration, exchange and autodiscover infrastructure  part 1#2  part 35#36Stage migration, exchange and autodiscover infrastructure  part 1#2  part 35#36
Stage migration, exchange and autodiscover infrastructure part 1#2 part 35#36Eyal Doron
 
Login and private message
Login and private messageLogin and private message
Login and private messagegueste832a8e
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIAhsanul Karim
 
Ivanti Cheat Sheet by Traversys Limited
Ivanti Cheat Sheet by Traversys LimitedIvanti Cheat Sheet by Traversys Limited
Ivanti Cheat Sheet by Traversys LimitedTim Read
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAhsanul Karim
 

What's hot (9)

Android Services
Android ServicesAndroid Services
Android Services
 
Inter Process Communication (IPC) in Android
Inter Process Communication (IPC) in AndroidInter Process Communication (IPC) in Android
Inter Process Communication (IPC) in Android
 
Servlet session 11
Servlet   session 11Servlet   session 11
Servlet session 11
 
Stage migration, exchange and autodiscover infrastructure part 1#2 part 35#36
Stage migration, exchange and autodiscover infrastructure  part 1#2  part 35#36Stage migration, exchange and autodiscover infrastructure  part 1#2  part 35#36
Stage migration, exchange and autodiscover infrastructure part 1#2 part 35#36
 
Login and private message
Login and private messageLogin and private message
Login and private message
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
 
Ivanti Cheat Sheet by Traversys Limited
Ivanti Cheat Sheet by Traversys LimitedIvanti Cheat Sheet by Traversys Limited
Ivanti Cheat Sheet by Traversys Limited
 
Multi attribute login feature
Multi attribute login featureMulti attribute login feature
Multi attribute login feature
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
 

Viewers also liked

Android AIDL Concept
Android AIDL ConceptAndroid AIDL Concept
Android AIDL ConceptCharile Tsai
 
Android with dagger_2
Android with dagger_2Android with dagger_2
Android with dagger_2Kros Huang
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...William Liang
 
就職 創業 即戰力_20161214
就職 創業 即戰力_20161214就職 創業 即戰力_20161214
就職 創業 即戰力_20161214信宏 陳
 
Android Media Player Development
Android Media Player DevelopmentAndroid Media Player Development
Android Media Player DevelopmentTalentica Software
 
Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)William Liang
 
The key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux KernelThe key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux KernelWilliam Liang
 
Android Transition
Android TransitionAndroid Transition
Android TransitionCharile Tsai
 
Android Training (Animation)
Android Training (Animation)Android Training (Animation)
Android Training (Animation)Khaled Anaqwa
 
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式信宏 陳
 
Introduction of Android View
Introduction of Android ViewIntroduction of Android View
Introduction of Android ViewCharile Tsai
 
Introducing Android Media Player
Introducing Android Media PlayerIntroducing Android Media Player
Introducing Android Media PlayerArif Huda
 
Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in BackgroundAhsanul Karim
 
Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introductionWilliam Liang
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIAhsanul Karim
 
Google Map Android API V2 setup guide
Google Map Android API V2 setup guideGoogle Map Android API V2 setup guide
Google Map Android API V2 setup guideCAVEDU Education
 

Viewers also liked (20)

Android AIDL Concept
Android AIDL ConceptAndroid AIDL Concept
Android AIDL Concept
 
Android with dagger_2
Android with dagger_2Android with dagger_2
Android with dagger_2
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...
 
就職 創業 即戰力_20161214
就職 創業 即戰力_20161214就職 創業 即戰力_20161214
就職 創業 即戰力_20161214
 
Android Animator
Android AnimatorAndroid Animator
Android Animator
 
Android Media Player Development
Android Media Player DevelopmentAndroid Media Player Development
Android Media Player Development
 
Android internals
Android internalsAndroid internals
Android internals
 
Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)Data-centric IoT (NTU CSIE 2016.12)
Data-centric IoT (NTU CSIE 2016.12)
 
The key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux KernelThe key issues for teaching or learning Android and Linux Kernel
The key issues for teaching or learning Android and Linux Kernel
 
Android Transition
Android TransitionAndroid Transition
Android Transition
 
Android Training (Animation)
Android Training (Animation)Android Training (Animation)
Android Training (Animation)
 
Android service
Android serviceAndroid service
Android service
 
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
 
Introduction of Android View
Introduction of Android ViewIntroduction of Android View
Introduction of Android View
 
Introducing Android Media Player
Introducing Android Media PlayerIntroducing Android Media Player
Introducing Android Media Player
 
Android Thread
Android ThreadAndroid Thread
Android Thread
 
Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in Background
 
Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introduction
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location API
 
Google Map Android API V2 setup guide
Google Map Android API V2 setup guideGoogle Map Android API V2 setup guide
Google Map Android API V2 setup guide
 

Similar to Android Service Fundamentals

Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentOwain Lewis
 
mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxNgLQun
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)Khaled Anaqwa
 
Android life cycle
Android life cycleAndroid life cycle
Android life cycle瑋琮 林
 
Andriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxAndriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxfaiz324545
 
Creating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 SeattleCreating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 SeattleJim McKeeth
 
Services I.pptx
Services I.pptxServices I.pptx
Services I.pptxRiziX3
 
Ts threading
Ts   threadingTs   threading
Ts threadingConfiz
 
DEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking ForDEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking ForMichael Scovetta
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8Utkarsh Mankad
 
Internals of AsyncTask
Internals of AsyncTask Internals of AsyncTask
Internals of AsyncTask BlrDroid
 
Systemd = init + inetd
Systemd = init + inetdSystemd = init + inetd
Systemd = init + inetdYoungChoonTae
 
Lotuscript for large systems
Lotuscript for large systemsLotuscript for large systems
Lotuscript for large systemsBill Buchan
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application DevelopmentAzfar Siddiqui
 

Similar to Android Service Fundamentals (20)

Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Android101
Android101Android101
Android101
 
mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptx
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)
 
Android life cycle
Android life cycleAndroid life cycle
Android life cycle
 
Andriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxAndriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptx
 
Creating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 SeattleCreating Android Services with Delphi and RAD Studio 10 Seattle
Creating Android Services with Delphi and RAD Studio 10 Seattle
 
Services I.pptx
Services I.pptxServices I.pptx
Services I.pptx
 
Aidl service
Aidl serviceAidl service
Aidl service
 
Ts threading
Ts   threadingTs   threading
Ts threading
 
DEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking ForDEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking For
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
 
9 services
9 services9 services
9 services
 
MDAD 6 - AIDL and Services
MDAD 6 - AIDL and ServicesMDAD 6 - AIDL and Services
MDAD 6 - AIDL and Services
 
Windows Services 101
Windows Services 101Windows Services 101
Windows Services 101
 
Internals of AsyncTask
Internals of AsyncTask Internals of AsyncTask
Internals of AsyncTask
 
Systemd = init + inetd
Systemd = init + inetdSystemd = init + inetd
Systemd = init + inetd
 
Lotuscript for large systems
Lotuscript for large systemsLotuscript for large systems
Lotuscript for large systems
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application Development
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Android Service Fundamentals

  • 2. Agenda • Basic Knowledge • Intent Service • Start/Stop Service • Bound Service • Isolated Service • Exported Service • Notification
  • 4. UIThread and WorkerThread • UIThread Only UIThread can control the UIWidgets Why? • WorkerThread Use WorkerThread to avoid blocking UI Some built-in tools help to use WorkerThread ex: Looper & Handler, AsyncTask.
  • 5. What is Service? • One of application components of Android Need to be declared in AndroidManefest.xml • Usually used to do long-tern background work • Service can interactive with other component (exported = true) • Service can (but not need to) run in another process • Service is running in UI Thread
  • 6. Service in AndroidManifest • android:name=“[package/service]” • android:enabled=“[true|false]” • android:exported=“[true|false]” • android:isolatedProcess=“[true|false]” • android:process=“[name/of/process]”
  • 7.
  • 9. Intent Service • It is implemented to do something in non-UI thread. • we only need to implement the WorkerThread • It only execute one at time (queueing) • It used HandlerThread • It will stop when completed
  • 10.
  • 12.
  • 13. • Can be started/stopped several times, but only one instance. (implicit singleton) • No start/stop counting • Service can stop itself. (call Service.stopSelf()) • If you need to start service for specified event, use Broadcast Receiver.
  • 14.
  • 15. int onStartCommand • The return value define the behavior of service • return START_STICKY • return START_NOT_STICKY • return START_REDELIVER_INTENT • flag: START_FLAG_REDELIVERY, START_FLAG_RETRY
  • 16. Start and Stop Service • Context.startService(Intent) • Context.stopService(Intent) • Service.stopSelf()
  • 17.
  • 19.
  • 20. Bind Service • A service can be bind several times. And service will maintain a bindCount • When someone bind service, bindCount + 1 • When someone unbind service, bindCount - 1 • Service destroy itself when bindCount == 0
  • 21. Context.bindService() • bindService(Intent, ServiceConnection, int flags); • flags: BIND_AUTO_CREATE BIND_NOT_FOREGROUND BIND_ABOVE_CLIENT BIND_ALLOW_OOM_MANAGEMENT BIND_WAIVE_PRIORITY
  • 22.
  • 23.
  • 25. Hyper Service • Service create: startService() or bindService() when no service created • Service destroy: if has been started: stopService()(or stopSelf()) && if has been bound: bindCount == 0
  • 26.
  • 28. Isolated Service • android:isolatedProcess=“true” • android:process=“:[process_name]” (Note: the “:” is needed) • Even you give two isolated processes the same process name, they will NOT run in same process. Thus, you get at least 3 process: app, service1, and service2 The process names of service1 and service2 are same
  • 29.
  • 30. IPC issues • # IPC = Inter Process Communication • Since the service is run in another process, we can not call its function directly • Solution: Messenger: Simple, but the calling order is not guarantee AIDL: Hard to implement, guarantee the calling order
  • 31. Messenger • Create a Handler to receive commands • Create a Messenger and use Handler • return Messenger.getBinder() in onBind() • disadvantage: The arguments cannot be customized The messenger system is asynchronized # function may not run immediately when called
  • 32.
  • 33.
  • 34. AIDL • the function call is synchronized # function call is blocked until return • This is too complexity so we don’t discuss here • Please check official document: http://developer.android.com/intl/zh- tw/guide/components/aidl.html
  • 36. Exported Service • Exported Service make you use other application’s service • The exported service is run in the process of its application, NOT in the process of caller • If used as bound service, the binder of it should support IPC (Thus, it usually used with AIDL.)
  • 37.
  • 39. Show Notification • Notification is part of Service • Use NotificationCompat.Builder • startForeground(int notificationId, Notification) # the notificationId must NOT be 0 • startForeground with same id will replace the previous notification which has same id.
  • 40.
  • 41. Conclusion • IntentService • Start / Stop Service • Bound Service (bind/unbind) • Isolated Service • Exported Service • Notification