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

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 (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

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 

Recently uploaded (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 

Android Service

  • 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