Java Reference
In-Depth Information
Note
Aserviceisnotaseparateprocess,althoughitcanbespecifiedtoruninasep-
arateprocess.Also,aserviceisnotathread.Instead,aserviceletstheapptellAndroid
aboutsomethingitwantstobedoinginthebackground(evenwhentheuserisnotdir-
ectlyinteractingwiththeapp),andletstheappexposesomeofitsfunctionalitytoother
apps.
Consideraservicethatplaysmusicinresponsetoauser'smusicchoiceviaanactiv-
ity.Theuserselectsthesongtoplayviathisactivity,andaserviceisstartedinresponse
totheselection. Therationale forusingaservice toplaythemusic isthat theuserex-
pectsthemusictokeepplayingevenaftertheactivitythatinitiatedthemusicleavesthe
screen.
The service plays the music on another thread to prevent the Application Not
Responding dialog box (see
Figure 12-4
) from appearing.
Figure 12-4.
The dreaded Application Not Responding dialog box may result in users uninstalling
the app.
Note
The abstract
android.app.Service
class implements services.
Manifest
Android learns about an app's various components (and more) by examining the app's
shows how this file might declare
Listing 12-1
's activity component.
Listing 12-2.
SimpleApp
's manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/
res/android"