SlideShare a Scribd company logo
Android Development
vol. 2
Tomáš Kypta
Tomáš Kypta
@TomasKypta
Agenda
• Service
• Broadcast receivers
• Libraries - Retrofit
• Fragments
• Asynchronous processing
Service
Service
• for long running tasks independent on UI
• also for potentially exposing some functionality to other
apps
• extend class android.app.Service
• two types:
• started service
• bound service
Service
• by default runs on the main thread!
Started Service
• to perform some operation without returning result
to the caller
• start by calling
context.startService(Intent)
• only one instance of the service is created
Bound Service
• for interacting with other components
• to expose functionality to other apps
• client calls bindService()
• cannot be called from broadcast receiver
Bound Service
• implement onBind()
• return null if you don’t want to allow binding
Bound Service
• clients call unbindService()
• when all clients are unbound, the system destroys
the service
• no need to stop service explicitly
Service
• Started and Bound approaches can be mixed
Service Lifecycle
• service lifetimes:
• entire lifetime
• active lifetime
• start in onStartCommand() or onBind()
Service
Lifecycle
Foreground Service
• something user is actively aware of
• must provide an ongoing notification
• cannot be dismissed
• makes app a higher priority process
• startForeground()
• stopForeground()
Intent Service
Intent Service
• service for processing on background threads
• for processing independent on UI
• android.app.IntentService
Intent Service
public class MyIntentService extends IntentService {



public MyIntentService() {

super("MyIntentService");

}



@Override

protected void onHandleIntent(Intent intent) {

// run some code on background

}

}
Exercise
1. Download the code: http://github.com/avast/
android-lectures
2. Import project in “Lecture 2” to Android studio
3. Run the app on device/emulator
4. Start DownloadService from MainActivity
5. Download User in a background thread in the
service, use GitHubApi class for that
Exercise
6. Start DownloadIntentService from MainActivity
7. Download list of repositories in the IntentService,
use GitHubApi class for that
Broadcast Receiver
Broadcast Receiver
• responds to broadcasts
• broadcasts are system wide
• receivers can be registered statically or dynamically
• system or custom broadcasts
• examples:
• incoming SMS, incoming call, screen turned off, low
battery, removed SD card, …
Broadcast Receiver
• extend class
android.content.BroadcastReceiver
• void onReceive(Context, Intent) callback
• called on the main thread
• don’t do any threading there!!
Broadcast Receiver
• static registration in AndroidManifest.xml
• <receiver> tag inside <application> tag
• publicly available
• to make private use
android:exported=“false”
Broadcast Receiver
• static registration not possible for all intents
• only some exceptions - can be found in
documentation
• e.g. ACTION_BATTERY_CHANGED
Broadcast Receiver
• dynamic registration
• Context.registerReceiver(BroadcastRe
ceiver, IntentFilter)
• Context.unregisterReceiver(Broadcast
Receiver)
Broadcasts
• normal
• completely asynchronous
• undefined order of called receivers
• ordered
• one receiver at a time
• android:priority
Sending Broadcasts
• Context.sendBroadcast(Intent)
• send to all apps registered for the broadcast
• can be restricted since ICS with
Intent.setPackage(String)
• Context.sendOrderedBroadcast(Intent,
String)
Exercise
8. Create BroadcastReceiver handling
ACTION_USER_DOWNLOADED action and
register/unregister it in onStart()/onStop()
9. Notify MainActivity about successful User
download from DownloadService via broadcast
Local Broadcast
Local Broadcast
LocalBroadcastManager lbManager =
LocalBroadcastManager.getInstance(context);
lbManager.registerReceiver(mReceiver, intentFilter);
lbManager.unregisterReceiver(mReceiver);


lbManager.sendBroadcast(intent);


lbManager.sendBroadcastSync(intent);
Local Broadcast
LocalBroadcastManager lbManager =
LocalBroadcastManager.getInstance(context);
lbManager.registerReceiver(mReceiver, intentFilter);
lbManager.unregisterReceiver(mReceiver);


lbManager.sendBroadcast(intent);


lbManager.sendBroadcastSync(intent);
Local Broadcast
LocalBroadcastManager lbManager =
LocalBroadcastManager.getInstance(context);
lbManager.registerReceiver(mReceiver, intentFilter);
lbManager.unregisterReceiver(mReceiver);


lbManager.sendBroadcast(intent);


lbManager.sendBroadcastSync(intent);
Local Broadcast
LocalBroadcastManager lbManager =
LocalBroadcastManager.getInstance(context);
lbManager.registerReceiver(mReceiver, intentFilter);
lbManager.unregisterReceiver(mReceiver);


lbManager.sendBroadcast(intent);


lbManager.sendBroadcastSync(intent);
Exercise
10.Create BroadcastReceiver handling
ACTION_REPOS_DOWNLOADED action and
register/unregister it in onStart()/onStop() via local
broadcast
11.Notify MainActivity about successful User
download from DownloadIntentService via local
broadcast
Notification
Notification
• a message that can be displayed to the user
outside normal UI
• displayed in notification area
Notification
• user can open notification drawer to see the details
• app can define UI and click action
Notification
• use NotificationCompat.Builder
Notification
NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.icon)

.setContentTitle("My notification")

.setContentText("Hello World!");
Notification
NotificationCompat.Builder mBuilder =

new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.icon)

.setContentTitle("My notification")

.setContentText("Hello World!");
Notification
Intent resultIntent = new Intent(this, MyActivity1.class);



TaskStackBuilder stackBuilder =
TaskStackBuilder.create(this);


stackBuilder.addParentStack(MyActivity1.class);

stackBuilder.addNextIntent(resultIntent);
Notification
Intent resultIntent = new Intent(this, MyActivity1.class);



TaskStackBuilder stackBuilder =
TaskStackBuilder.create(this);


stackBuilder.addParentStack(MyActivity1.class);

stackBuilder.addNextIntent(resultIntent);
Notification
Intent resultIntent = new Intent(this, MyActivity1.class);



TaskStackBuilder stackBuilder =
TaskStackBuilder.create(this);


stackBuilder.addParentStack(MyActivity1.class);

stackBuilder.addNextIntent(resultIntent);
Notification
PendingIntent resultPendingIntent =

stackBuilder.getPendingIntent(

0,

PendingIntent.FLAG_UPDATE_CURRENT

);

mBuilder.setContentIntent(resultPendingIntent);


NotificationManager mNotificationManager =

(NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);


mNotificationManager.notify(MY_ID, mBuilder.build());
Notification
PendingIntent resultPendingIntent =

stackBuilder.getPendingIntent(

0,

PendingIntent.FLAG_UPDATE_CURRENT

);

mBuilder.setContentIntent(resultPendingIntent);


NotificationManager mNotificationManager =

(NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);


mNotificationManager.notify(MY_ID, mBuilder.build());
Notification
PendingIntent resultPendingIntent =

stackBuilder.getPendingIntent(

0,

PendingIntent.FLAG_UPDATE_CURRENT

);

mBuilder.setContentIntent(resultPendingIntent);


NotificationManager mNotificationManager =

(NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);


mNotificationManager.notify(MY_ID, mBuilder.build());
Notification
• update notification by using the same ID
Exercise
12.Create notification showing repos count when we
are notified about downloaded data.
13.Create click action for the notification that will
open ReposActivity
14.Create back stack in the click action containing
MainActivity
Application
Application
• application singleton
• onCreate() lifecycle callback
• for app initialisations
Libraries
Retrofit
• for simple declaration of RESTful API
• create an interface with annotations
• create an instance fo the interface via RestAdapter
Retrofit
• support for many formats via converters
• json, xml, protocol buffers, …
• supports sync and async download
• and more …
Exercise
15.Create Retrofit API method for getting repository
contributors
• https://developer.github.com/v3/repos/#list-
contributors
Fragment
Fragment
• represents a behavior or a portion of user interface
in an activity
• multiple fragments can be combined in a single
activity
Fragment
Activity & Fragment
• add in the layout
<fragment android:name="com.example.MyListFragment"
android:id="@+id/list"
android:layout_width="100dp"
android:layout_height="match_parent" />
Activity & Fragment
• add programmatically
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction transaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
Activity & Fragment
• add programmatically
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction transaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
Activity & Fragment
• add programmatically
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction transaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
Activity & Fragment
• add programmatically
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction transaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
Activity & Fragment
• add programmatically
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction transaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
Fragment Back Stack
• fragments can be kept in a stack
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction transaction =
fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
Fragment Back Stacks
A A
B
A
B
C
A
B
back
Fragment Lifecycle
Fragment Lifecycle
• a bit more complicated than activity lifecycle
Fragment
Lifecycle
Fragment
Lifecycle
Activity &
Fragment
Lifecycle
Activity &
Fragment
Lifecycle
Fragment Lifecycle
Callbacks
onAttach()
• fragments associated with the activity
onCreateView()
• create fragment’s view hierarchy here
onActivityCreated()
• activity’s onCreate() method has returned
Fragment Lifecycle
Callbacks
onDestroyView()
• view hierarchy is being removed
onDetach()
• fragment is being disassociated from the activity
Fragment without a UI
• aka worker fragment
transaction.add(workFragment, “work”);
Exercise
16.Create Activity with dynamically added Fragment
Background
Processing
Threads
• main thread = UI thread
• never block the UI thread!!!
• use worker threads for time consuming operations
• networking, db, filesystem, …
• UI toolkit is not thread safe
• never manipulate UI from a worker thread!!!
Threads
• complications
• activities are restarted
• memory leaks
• crashes
Background Processing
• Thread
• AsyncTask
• IntentService
• Loader
• ThreadPoolExecutor
• AbstractThreadedSyncAdapter
Background Processing
Some people, when confronted with a
problem, think, "I know, I'll use threads,"
and then two they hav erpoblesms.
HandlerThread
HandlerThread
• holds a queue of tasks
• other threads can push tasks to it
• the thread processes its queue, one task after
another
• when the queue is empty, it blocks until something
appears
Looper and Handler
• Looper
• class that runs a message loop for a thread
• Handler
• provides interaction with the message loop
Looper and Handler
• sendMessage(Message)
• Message object, retrieve with
Message.obtain()
• post(Runnable)
• delayed versions, at time versions
Looper and Handler
• UI thread has Looper
• you can create easily another Handler
• Handler is bound to the Looper of the current
thread
• or you can explicitly provide different Looper
Loader
Loader
• asynchronous loading of data
• bound to activity or fragment
• monitor source of data, deliver changes
• reconnect after config change, don’t need to
requery
• managed by LoaderManager
Loader
• AsyncTaskLoader
• CursorLoader
Loader
• you have to implement
LoaderManager.LoaderCallbacks
Loader
@Override

public Loader<D> onCreateLoader(int id, Bundle args) {
// instantiate a new loader

return null;

}



@Override

public void onLoadFinished(Loader<D> loader, D data) {

// called when loader finished its loading

}



@Override

public void onLoaderReset(Loader<D> loader) {

// called when loader is being reset

}
Loader
@Override

public Loader<D> onCreateLoader(int id, Bundle args) {
// instantiate a new loader

return null;

}



@Override

public void onLoadFinished(Loader<D> loader, D data) {

// called when loader finished its loading

}



@Override

public void onLoaderReset(Loader<D> loader) {

// called when loader is being reset

}
Loader
@Override

public Loader<D> onCreateLoader(int id, Bundle args) {
// instantiate a new loader

return null;

}



@Override

public void onLoadFinished(Loader<D> loader, D data) {

// called when loader finished its loading

}



@Override

public void onLoaderReset(Loader<D> loader) {

// called when loader is being reset

}
Loader
@Override

public Loader<Cursor> onCreateLoader(int id, Bundle args) {

return new CursorLoader(getActivity(), mUri,
mProjection, "value > ?",
new String[]{String.valueOf(5)}, "value ASC”);

}
Loader
@Override

public void onLoadFinished(Loader<Cursor> loader, Cursor
cursor) {

mAdapter.swapCursor(cursor);

}



@Override

public void onLoaderReset(Loader<Cursor> loader) {

mAdapter.swapCursor(null);

}
Loader
@Override

public void onLoadFinished(Loader<Cursor> loader, Cursor
cursor) {

mAdapter.swapCursor(cursor);

}



@Override

public void onLoaderReset(Loader<Cursor> loader) {

mAdapter.swapCursor(null);

}
Loader
// prepare the loader

// either re-connect with an existing one,

// or start a new one.
getLoaderManager().initLoader(0, null, this);
Loader
// restart the loader to do a new query


getLoaderManager().restartLoader(0, null, this);
Exercise
17.Load repository contributors via loader in the
Fragment and display the data
18.Create statically defined receiver that will show
toast when airplane mode is enabled/disabled -
action android.intent.action.AIRPLANE_MODE
THE END

More Related Content

PDF
Writing testable Android apps
PDF
Android with dagger_2
PDF
Angular 2: core concepts
PPTX
Sword fighting with Dagger GDG-NYC Jan 2016
PDF
Angular Dependency Injection
PDF
Angular 2 - The Next Framework
PDF
Advanced Dagger talk from 360andev
Writing testable Android apps
Android with dagger_2
Angular 2: core concepts
Sword fighting with Dagger GDG-NYC Jan 2016
Angular Dependency Injection
Angular 2 - The Next Framework
Advanced Dagger talk from 360andev

What's hot (20)

PDF
Using Dagger in a Clean Architecture project
PPTX
Dagger 2
PDF
Dagger2 Intro
PDF
Building maintainable app
PDF
Epoxy 介紹
PDF
Angular2 workshop
PDF
Data Flow Patterns in Angular 2 - Sebastian Müller
PPTX
Dagger 2 ppt
PDF
The productive developer guide to Angular 2
PDF
Android talks #08 dagger2
PDF
基於 Flow & Path 的 MVP 架構
PDF
Commit University - Exploring Angular 2
ODP
Introduction to Angular 2
PDF
Angular2 with TypeScript
PPTX
Angular
PDF
Angular 2 Essential Training
PDF
DI with Dagger2
PDF
An introduction to Angular2
PDF
Automated Testing in Angular Slides
PDF
Tech Webinar: Angular 2, Introduction to a new framework
Using Dagger in a Clean Architecture project
Dagger 2
Dagger2 Intro
Building maintainable app
Epoxy 介紹
Angular2 workshop
Data Flow Patterns in Angular 2 - Sebastian Müller
Dagger 2 ppt
The productive developer guide to Angular 2
Android talks #08 dagger2
基於 Flow & Path 的 MVP 架構
Commit University - Exploring Angular 2
Introduction to Angular 2
Angular2 with TypeScript
Angular
Angular 2 Essential Training
DI with Dagger2
An introduction to Angular2
Automated Testing in Angular Slides
Tech Webinar: Angular 2, Introduction to a new framework
Ad

Viewers also liked (7)

PDF
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
PDF
Guide to the jungle of testing frameworks
PDF
Guide to the jungle of testing frameworks
PDF
Android Develpment vol. 3, MFF UK, 2015
PDF
Reactive programming on Android
PDF
Practical RxJava for Android
PDF
Practical RxJava for Android
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
Android Develpment vol. 3, MFF UK, 2015
Reactive programming on Android
Practical RxJava for Android
Practical RxJava for Android
Ad

Similar to Android Develpment vol. 2, MFF UK, 2015 (20)

PDF
Being Epic: Best Practices for Android Development
PDF
Pragmatic Monolith-First, easy to decompose, clean architecture
PDF
DEFCON 18- These Aren't the Permissions You're Looking For
PDF
The Ring programming language version 1.2 book - Part 51 of 84
PDF
Building Top-Notch Androids SDKs
PDF
Android101
PDF
What's new in Android O
PDF
Automatically Managing Service Dependencies in an OSGi Environment - Marcel O...
PDF
React Native: The Development Flow
PPTX
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
PDF
Jollen's Presentation: Introducing Android low-level
PDF
Improving app performance with Kotlin Coroutines
PPT
Android应用开发简介
PPTX
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
PPTX
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
PPT
Gwt and rpc use 2007 1
PDF
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
ZIP
Building Web Apps Sanely - EclipseCon 2010
PDF
Ekon25 mORMot 2 Server-Side Notifications
PDF
TypeScript for Java Developers
Being Epic: Best Practices for Android Development
Pragmatic Monolith-First, easy to decompose, clean architecture
DEFCON 18- These Aren't the Permissions You're Looking For
The Ring programming language version 1.2 book - Part 51 of 84
Building Top-Notch Androids SDKs
Android101
What's new in Android O
Automatically Managing Service Dependencies in an OSGi Environment - Marcel O...
React Native: The Development Flow
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Jollen's Presentation: Introducing Android low-level
Improving app performance with Kotlin Coroutines
Android应用开发简介
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
Gwt and rpc use 2007 1
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Building Web Apps Sanely - EclipseCon 2010
Ekon25 mORMot 2 Server-Side Notifications
TypeScript for Java Developers

More from Tomáš Kypta (14)

PDF
Modern Android app library stack
PDF
ProGuard
PDF
Unit testing and Android
PDF
Android Development for Phone and Tablet
PDF
Reactive programming on Android
PDF
Android development - the basics, MFF UK, 2014
PDF
Android Libraries
PDF
Android Development 201
PDF
Android development - the basics, MFF UK, 2013
PDF
Užitečné Android knihovny pro vývoj a testování
PDF
Programování pro Android - úvod, FI MUNI, 2013
PDF
Stylování ActionBaru
PDF
Android development - the basics, MFF UK, 2012
PDF
Android development - the basics, FI MUNI, 2012
Modern Android app library stack
ProGuard
Unit testing and Android
Android Development for Phone and Tablet
Reactive programming on Android
Android development - the basics, MFF UK, 2014
Android Libraries
Android Development 201
Android development - the basics, MFF UK, 2013
Užitečné Android knihovny pro vývoj a testování
Programování pro Android - úvod, FI MUNI, 2013
Stylování ActionBaru
Android development - the basics, MFF UK, 2012
Android development - the basics, FI MUNI, 2012

Recently uploaded (20)

PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Trump Administration's workforce development strategy
PPTX
master seminar digital applications in india
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Lesson notes of climatology university.
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Yogi Goddess Pres Conference Studio Updates
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Anesthesia in Laparoscopic Surgery in India
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
UNIT III MENTAL HEALTH NURSING ASSESSMENT
01-Introduction-to-Information-Management.pdf
Cell Structure & Organelles in detailed.
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
Trump Administration's workforce development strategy
master seminar digital applications in india
2.FourierTransform-ShortQuestionswithAnswers.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Final Presentation General Medicine 03-08-2024.pptx
Lesson notes of climatology university.
Supply Chain Operations Speaking Notes -ICLT Program
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE

Android Develpment vol. 2, MFF UK, 2015