Extending Service class. This is using same UI thread by default. So if we do not run our methods in separate threads, we can notice that UI may be lagged, since it is sharing same thread.
starting and stopping the service
This one is running methods in separate worker thread by default. So it might be a better start point for using services.
registering service in AndroidManifest.xml
Started Services – service that downloads some information in the background.
Bound Services – programmatic interface for callers to interact with, such as a location service.
Create a Started Service
Create a Bound Service
Value is of type
StartCommandResult, which can be any of the following:
Sticky– A sticky service will be restarted, and a null intent will be delivered to
OnStartCommandat restart. Used when the service is continuously performing a long-running operation, such as updating a stock feed.
RedeliverIntent– The service is restarted, and the last intent that was delivered to
OnStartCommandbefore the service was stopped by the system is redelivered. Used to continue a long-running command, such as the completion of a large file upload.
NotSticky– The service is not automatically restarted.
StickyCompatibility– Restart will behave like
Stickyon API level 5 or greater, but will downgrade to pre-level 5 behavior on earlier versions.
In order to conserve system resources, a started service implementation calls
StopSelf after any long-running work is done. This is important because the service will run independently of the component that called
can be stopped from outside call as well
When a service is stopped, the
OnDestroy method will be called on the service. This is the part of the process where any cleanup of service-wide resources can be done. To provide an implementation, simply override
OnDestroy as follows:
To call a service in either the local or remote scenario, use intent filters.
With this attribute in place, the service can be started from any class that inherits from
Context, For example,
Activity uses the following code, and passes the action to the intent:
it’s possible to create some services that will not be destroyed by the system under normal circumstances. Services of this kind are known as foreground services. Users are actively aware of the presence of foreground services. For example, an Internet radio service is a good candidate to be created as a foreground service because the user is aware of its existence through the audio it plays back.
IntentService works by sending all intents to a worker queue for processing. This queue processes each intent serially on a separate thread, so as to not block the main thread, passing the intent to the
OnHandleIntent method. When all the intents have been processed, the
IntentService stops itself by calling
Unlike Started Services, Bound Services do not run indefinitely. Instead, they are created when a client connects to them and are destroyed after all bound clients have disconnected.
The following diagram illustrates the lifecycle of a Bound Service:
Android Interface Definition Language (AIDL)
AIDL (Android Interface Definition Language). It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC). On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.
Android uses the
AlarmManager class to allow an application to schedule itself to be run at certain times in the future.
The hybrid approach is very powerful and common way to use Services.
Hybrid Service: The lifecycle of both Bound and Started Services can be combined by explicitly starting a Service and then binding to it. This offers the advantage of starting a Service independently of the rest of the application to provide it with an opportunity to do work, and then to also be able to connect to it directly in order to access it.
Since the introduction of Service Messengers, AIDL has largely been superseded, because Service Messengers are much simpler to use and easier to implement.
Any component that inherits from
Context (such as an
Activity) can start the Service.
the following code snippet shows an
OnStartCommand implementation that returns
StartResultCommand.Sticky, which will restart the Service automatically. A Sticky Service will get restarted with a null intent if the OS ever shuts it down due to memory pressure.
The following code calls the
StopSelf method after it completes its work.
The following diagram illustrates how the caller uses a
ServiceConnection to communicate with a Service running locally:
To get location data from the system to the Service, we need to use a
LocationManager. The location manager provides a way for the application to interact with the system location Service.
StopSelf does not work if the binding is still active or if we are still registered to receive location updates from the Location Manager. Remember to stop location updates in the LocationService
Android Backgrounding Guidance
MediaPlayer is very state sensitive and calling one of its methods in the wrong state will cause an exception to be thrown.
List views and adapters are one of the most fundamental building blocks of Android Applications.
Fragments are classes that represent a portion of the user interface in an Activity.
Auto Complete Sample
calendar info displayed in the
<ListView android:id="@android:id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/calDisplayName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dip" /> <TextView android:id="@+id/calAccountName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12dip" /> </LinearLayout>
Adding a Calendar Event
CardView is used to present a single row item in a
Creating a Splash Screen