131 lines
3.5 KiB
Markdown
131 lines
3.5 KiB
Markdown
# Services
|
|
|
|
### Uses of services
|
|
|
|
- MP3 Playback
|
|
- Playing songs in the background
|
|
- Network Access
|
|
- Long downloads, sending emails, polling an email server for new mail, etc.
|
|
|
|
### Services
|
|
|
|
An application **component** that
|
|
|
|
- Has no UI
|
|
- Represents a desire to perform a longer-running operation
|
|
|
|
Activities are loaded and unloaded as users move around the app
|
|
|
|
- Services remain for as long as they are needed
|
|
- One service may be used by many other applications to avoid duplication of resources.
|
|
|
|
### What are Services *not*?
|
|
|
|
**Not** a separate process - runs in the same process as the application in which it is declared
|
|
|
|
**Not** a thread - there's one thread per application
|
|
|
|
Services are simple
|
|
|
|
- It's a way of telling the system about a part of your app that is expected to run for a long time
|
|
- i.e. longer than a few seconds
|
|
|
|
### Tasks, Services and Activities
|
|
|
|
###### Email
|
|
|
|
- Checks for new mail occasionally -> Service
|
|
- Collects new mail and stores it -> Service
|
|
- Notifies user that there is new mail -> Service / Activity
|
|
- New component - **notifications**
|
|
- User switches to inbox and retrieves/views new mail -> Activity
|
|
|
|
###### MP3 Playback task
|
|
|
|
- Play music while user does something else -> Service
|
|
- Create a playlist -> Activity
|
|
- Change the song playing & display current song progress -> Service / Activity
|
|
|
|
### Creating a Service
|
|
|
|
- Services are designed to support communication with
|
|
- Local activities (in the same process)
|
|
- within VM
|
|
- Remote activities
|
|
- IPC (Inter-process communication)
|
|
- Multiple components
|
|
- providing a service for multiple clients
|
|
|
|
Services are components, similar to an activity
|
|
|
|
- Register the service in the manifest
|
|
- Create a subclass of `android.app.Service`
|
|
- Handle the relevant life cycle methods
|
|
|
|
### Service Life Cycle
|
|
|
|
- By nature, services are **singleton objects**
|
|
- A service might be used by many clients
|
|
- A service is started by something
|
|
- via an `intent`
|
|
- The service subclass object is instantiated if necessary
|
|
- `onCreate()` is called
|
|
- either `onStartCommand` or `onBind` will be called depending on how the service has been invoked
|
|
- `onCreate()`/`onStart()`/`onBind()` are called in the context of the main UI thread
|
|
- A single thread for all components in an application
|
|
- Must spawn a worker thread
|
|
- Something calls `stopService()`
|
|
- Could be the service, one of the clients (the activity) or the OS
|
|
- `onDestroy()`
|
|
|
|
## Code Examples
|
|
|
|
`MainActivity` class
|
|
|
|
```java
|
|
public class MainActivity extends AppCompatActivity {
|
|
@Override
|
|
protected void onCreate(Bundle ...) {
|
|
...
|
|
}
|
|
|
|
//if clicked twice - onStartCommand is called, onCreate is called ONCE
|
|
public void onClickStartService(View v){
|
|
this.startService(new Intent(MainActivity.this, SimpleService.class));
|
|
}
|
|
|
|
public void onClickStopService(View v){
|
|
this.stopService(new Intent(MainActivity.this, SimpleService.class));
|
|
}
|
|
|
|
public void onClickClobberService(View v) {
|
|
android.os.Process.killProcess(android.os.Process.myPid());
|
|
}
|
|
}
|
|
```
|
|
|
|
`SimpleService` class
|
|
|
|
```java
|
|
public class SimpleService extends Service {
|
|
//called first
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
}
|
|
|
|
//called second
|
|
public int onStartCommand(Intent i, int flags, int startId) {
|
|
return Service.START_REDELIVER_INTENT;
|
|
}
|
|
}
|
|
```
|
|
|
|
Android Manifest
|
|
|
|
```xml
|
|
<service
|
|
android:name=".SimpleService"
|
|
android:exported="false">
|
|
</service>
|
|
```
|