SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Welcome
Android: What’s running in Background
Road to perdition
Hello world
UI Thread (ANR)
Worker Thread (CalledFromWrongThreadException)
Handler AsyncTask (Component Destroyed)
Better Call Service
Service (when no one is looking)
Services / Threads (war of two worlds)
A service is not an alternative to background threads but it provides
another scope in which to run your worker threads!
• The thread should stay alive, even when the user is not
interacting with the application.
• In addition, a Service can be started from another
application using an Intent.
Golden Rule
DO NOT let services live longer
than they are needed.
Options
GCM
BroadcastReceiver
LocalBroadcastReceiver
WakefulBroadcastReceiver
HandlerThreads
AsyncTaskLoaders
ThreadPoolExecutor
ContentProvider
IntentService
SyncAdapter
JobScheduler
DownloadManager
Services
Normal Service
Intent Service [special case of normal service]
Bound Service [in same process]
Bound Service [in separate process-> Messenger]
Bound Service [in separate application-> AIDL]
onStartCommand
START_STICKY to automatically restart if killed, to keep it active.
START_REDELIVER_INTENT for auto restart and retry if the service was killed before
stopSelf(). This is needed for services receiving inputs.
START_NOT_STICKY to not automatically restarts if killed, but wait for next
startService() call. For non-essential services.
START_STICKY_COMPATIBILITY with Android 1.6 and earlier legacy onStart()
method.
Start Services
• What happens if task is removed from recent?
• What happens if force stop?
• What happens if exception occur in running process?
Local Bound Services
What would happen if try to make connect IPC in LocalBinder?
Sync on Android
Well, you probably use it everyday...
Big picture
Big picture
Account manager
Single sign-in
Secure storage
Share auth-token
Ref: http://blog.udinic.com/2013/04/24/write-your-own-
android-authenticator/Account manager
Content provider
Common storage
CRUD operations
Require permissions
ContactsProvider:
<uses-permission android:name="android.permission.READ_CONTACTS">
<uses-permission android:name="android.permission.WRITE_CONTACTS">
setIsSyncable (Account account, String authority, int syncable)
setSyncAutomatically (Account account, String authority, boolean sync)
addPeriodicSync (Account account, String authority, Bundle extras, long pollFrequency)
requestSync (Account account, String authority, Bundle extras)
How the sync adapter works
Download manager
Run in a background
Network type check
Maintain queue
Job Scheduler
❏Introduced in Android 5.0 Lollipop (API 21)
❏When you would use this
- Tasks that should be done once the device is connected to a power supply
- Tasks that require network access or a Wi-Fi connection.
- Task that are not critical or user facing
- Tasks that should be running on a regular basis as batch where the timing is
not critical
❏ Job Scheduler
- Persists the data across device reboots
- Certain criteria can be set
- Service runs when set criteria is available
- Delay can be set
- Use for periodical running task
❏ Alarm Manager
- Broadcast listener to wake up
- Data get lost on device reboot
- Service keeps running until you manually
turns off or system do it for you
- Use for scheduling task at specific time

Weitere ähnliche Inhalte

Ähnlich wie Android Whats running in background

Asynchronous Programming in Android
Asynchronous Programming in AndroidAsynchronous Programming in Android
Asynchronous Programming in AndroidJohn Pendexter
 
"Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap...
"Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap..."Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap...
"Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap...Fwdays
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)Khaled Anaqwa
 
Andriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxAndriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxfaiz324545
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & DatabasesMuhammad Sajid
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1Utkarsh Mankad
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart JfokusLars Vogel
 
Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...
Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...
Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...Amazon Web Services
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAnuradha Weeraman
 
Android 101 Session @thejunction32
Android 101 Session @thejunction32Android 101 Session @thejunction32
Android 101 Session @thejunction32Eden Shochat
 
Process Management in Android
Process Management in AndroidProcess Management in Android
Process Management in AndroidShrey Verma
 
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...MongoDB
 
Android Programming made easy
Android Programming made easyAndroid Programming made easy
Android Programming made easyLars Vogel
 
Android 3.1 - Portland Code Camp 2011
Android 3.1 - Portland Code Camp 2011Android 3.1 - Portland Code Camp 2011
Android 3.1 - Portland Code Camp 2011sullis
 
UA Mobile 2012 (English)
UA Mobile 2012 (English)UA Mobile 2012 (English)
UA Mobile 2012 (English)dmalykhanov
 
Android sync adapter
Android sync adapterAndroid sync adapter
Android sync adapterAlex Tumanoff
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)Google
 
Developing Android Client Apps via SyncAdapter
Developing Android Client Apps via SyncAdapterDeveloping Android Client Apps via SyncAdapter
Developing Android Client Apps via SyncAdapterAnatoliy Kaverin
 
Serverless Design Patterns (London Dev Community)
Serverless Design Patterns (London Dev Community)Serverless Design Patterns (London Dev Community)
Serverless Design Patterns (London Dev Community)Yan Cui
 

Ähnlich wie Android Whats running in background (20)

Asynchronous Programming in Android
Asynchronous Programming in AndroidAsynchronous Programming in Android
Asynchronous Programming in Android
 
"Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap...
"Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap..."Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap...
"Micro-frontends from A to Z. How and Why we use Micro-frontends in Namecheap...
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)
 
Andriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptxAndriod Lecture 8 A.pptx
Andriod Lecture 8 A.pptx
 
Data Transfer between Activities & Databases
Data Transfer between Activities & DatabasesData Transfer between Activities & Databases
Data Transfer between Activities & Databases
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1
 
Android Jumpstart Jfokus
Android Jumpstart JfokusAndroid Jumpstart Jfokus
Android Jumpstart Jfokus
 
Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...
Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...
Implementare e gestire soluzioni per l'Internet of Things (IoT) in modo rapid...
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the Trenches
 
Android 101 Session @thejunction32
Android 101 Session @thejunction32Android 101 Session @thejunction32
Android 101 Session @thejunction32
 
Chat Done Right
Chat Done Right Chat Done Right
Chat Done Right
 
Process Management in Android
Process Management in AndroidProcess Management in Android
Process Management in Android
 
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
MongoDB World 2018: Ch-Ch-Ch-Ch-Changes: Taking Your Stitch Application to th...
 
Android Programming made easy
Android Programming made easyAndroid Programming made easy
Android Programming made easy
 
Android 3.1 - Portland Code Camp 2011
Android 3.1 - Portland Code Camp 2011Android 3.1 - Portland Code Camp 2011
Android 3.1 - Portland Code Camp 2011
 
UA Mobile 2012 (English)
UA Mobile 2012 (English)UA Mobile 2012 (English)
UA Mobile 2012 (English)
 
Android sync adapter
Android sync adapterAndroid sync adapter
Android sync adapter
 
What's new in android jakarta gdg (2015-08-26)
What's new in android   jakarta gdg (2015-08-26)What's new in android   jakarta gdg (2015-08-26)
What's new in android jakarta gdg (2015-08-26)
 
Developing Android Client Apps via SyncAdapter
Developing Android Client Apps via SyncAdapterDeveloping Android Client Apps via SyncAdapter
Developing Android Client Apps via SyncAdapter
 
Serverless Design Patterns (London Dev Community)
Serverless Design Patterns (London Dev Community)Serverless Design Patterns (London Dev Community)
Serverless Design Patterns (London Dev Community)
 

Android Whats running in background

Hinweis der Redaktion

  1. this need to be consider thread is a java object which mostly runs on android component. Android component has lifecycle java object don’t as such. there could be possibility of leakage if thread outives android component. Go to diagram You should always create a separate thread within a service if you are doing something more complicated. The thread should stay alive, even when the user is not interacting with the application. In addition, a Service can be started from another application using an Intent.
  2. Performnace issues Memory issues Battery drain Inconsitent data
  3. Shift to code
  4. You need to identify and understand what problem you are facing. Because there are other tools which might perform the task; before even you start thinking you need your service.
  5. For non sticky service don’t re launch. For sticky service is re launch with different start id and NULL intent. For redelivery same start id and previous intent. Nobody relaunches Hack use broadcast receiver at onDestroy() but very bad userExperience sticky restart but intent is null so becomes non sticky Non sticky don’t restart Redelivery pass same intent which was sent before
  6. java.lang.ClassCastException: android.os.BinderProxy at edu.navalkishoreb.androidservices.connectors.LocalConnection.onServiceConnected