SlideShare a Scribd company logo
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

Android Services
Android ServicesAndroid Services
Android Services
Ahsanul Karim
 
Inter Process Communication (IPC) in Android
Inter Process Communication (IPC) in AndroidInter Process Communication (IPC) in Android
Inter Process Communication (IPC) in Android
Malwinder Singh
 
Servlet session 11
Servlet   session 11Servlet   session 11
Servlet session 11
Anuj Singh Rajput
 
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
Eyal Doron
 
Login and private message
Login and private messageLogin and private message
Login and private message
gueste832a8e
 
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
Ahsanul Karim
 
Ivanti Cheat Sheet by Traversys Limited
Ivanti Cheat Sheet by Traversys LimitedIvanti Cheat Sheet by Traversys Limited
Ivanti Cheat Sheet by Traversys Limited
Tim Read
 
Multi attribute login feature
Multi attribute login featureMulti attribute login feature
Multi attribute login feature
Chathuranga Priyadarshana
 
Android Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver TutorialAndroid Application Component: BroadcastReceiver Tutorial
Android Application Component: BroadcastReceiver Tutorial
Ahsanul 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 Concept
Charile Tsai
 
Android with dagger_2
Android with dagger_2Android with dagger_2
Android with dagger_2
Kros 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 Animator
Android AnimatorAndroid Animator
Android Animator
Charile Tsai
 
Android Media Player Development
Android Media Player DevelopmentAndroid Media Player Development
Android Media Player Development
Talentica Software
 
Android internals
Android internalsAndroid internals
Android internals
Liran Ben Haim
 
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 Kernel
William Liang
 
Android Transition
Android TransitionAndroid Transition
Android Transition
Charile Tsai
 
Android Training (Animation)
Android Training (Animation)Android Training (Animation)
Android Training (Animation)
Khaled Anaqwa
 
Android service
Android serviceAndroid service
Android service
Kirill Rozov
 
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式
不同尺寸與解析度的螢幕下,Android 程式 UI 的設計與解決方式信宏 陳
 
Introduction of Android View
Introduction of Android ViewIntroduction of Android View
Introduction of Android View
Charile Tsai
 
Introducing Android Media Player
Introducing Android Media PlayerIntroducing Android Media Player
Introducing Android Media Player
Arif Huda
 
Android Thread
Android ThreadAndroid Thread
Android Thread
Charile Tsai
 
Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in Background
Ahsanul Karim
 
Android device driver structure introduction
Android device driver structure introductionAndroid device driver structure introduction
Android device driver structure introduction
William 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 API
Ahsanul 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 guide
CAVEDU 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 Development
Owain Lewis
 
Android101
Android101Android101
Android101
David Marques
 
mobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptxmobile development with androiddfdgdfhdgfdhf.pptx
mobile development with androiddfdgdfhdgfdhf.pptx
NgLQun
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
Muhammad 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.pptx
faiz324545
 
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
Jim McKeeth
 
Services I.pptx
Services I.pptxServices I.pptx
Services I.pptx
RiziX3
 
Aidl service
Aidl serviceAidl service
Aidl service
Anjan Debnath
 
Ts threading
Ts   threadingTs   threading
Ts threading
Confiz
 
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
Michael 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 - day8
Utkarsh Mankad
 
9 services
9 services9 services
9 services
Ajayvorar
 
MDAD 6 - AIDL and Services
MDAD 6 - AIDL and ServicesMDAD 6 - AIDL and Services
MDAD 6 - AIDL and Services
Alexandru Radovici
 
Windows Services 101
Windows Services 101Windows Services 101
Windows Services 101
Nurul Haszeli Ahmad
 
Internals of AsyncTask
Internals of AsyncTask Internals of AsyncTask
Internals of AsyncTask
BlrDroid
 
Systemd = init + inetd
Systemd = init + inetdSystemd = init + inetd
Systemd = init + inetd
YoungChoonTae
 
Lotuscript for large systems
Lotuscript for large systemsLotuscript for large systems
Lotuscript for large systems
Bill Buchan
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application Development
Azfar 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

What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
kalichargn70th171
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
Drona Infotech
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Codeigniter VS Cakephp Which is Better for Web Development.pdf
Codeigniter VS Cakephp Which is Better for Web Development.pdfCodeigniter VS Cakephp Which is Better for Web Development.pdf
Codeigniter VS Cakephp Which is Better for Web Development.pdf
Semiosis Software Private Limited
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 

Recently uploaded (20)

What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Codeigniter VS Cakephp Which is Better for Web Development.pdf
Codeigniter VS Cakephp Which is Better for Web Development.pdfCodeigniter VS Cakephp Which is Better for Web Development.pdf
Codeigniter VS Cakephp Which is Better for Web Development.pdf
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 

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