Autostarting services

Tutorials with advanced 'difficulty' and more Lines of Code.

Autostarting services

Postby paller » Fri Feb 29, 2008 8:58 pm

Click here for the cross-linked version of this entry

Some privileged services start automatically in Android while the lowly applications I have written so far had to be started manually. I was not satisfied with the situation so I went after the Android bootup sequence.

The Linux layer under the JVM boots up normally, using the init script but I was not interested in that. I found that the Java services are all started by the System server (android.server.SystemServer) which does launch each system server in a hardwired way.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ...
  3.  
  4. Log.i("SystemServer", "Starting Alarm Manager.");
  5.  
  6. sm.addService("alarm", new AlarmManagerService(context));
  7.  
  8. Log.i("SystemServer", "Starting Window Manager.");
  9.  
  10. wm = WindowManagerService.main(context, power);
  11.  
  12. sm.addService("window", wm);
  13.  
  14. ...
  15.  
  16.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


There is no way getting our service autostarted this way but fortunately there is the android.intent.action.BOOT_COMPLETED intent which is broadcasted when the boot completes. One needs android.permission.RECEIVE_BOOT_COMPLETED permission to receive this intent.

Click here to download the example program

The example program has two parts: an intent receiver catching the BOOT_COMPLETED intent and a primitive service that counts down from 10 in every 3 seconds and logs the counter value. When it counts down, the service dies completely, this is necessary as there is no simple way to uninstall packages in the SDK so this service is not allowed to cause trouble later. Please, note the use of Handler for the timed countdown: you can't stay for too long in onStart() otherwise the application manager will shoot down the service.

Check the boot log below and you will find how the service is started. Each ... represents deleted log entries because meanwhile the boot process is happening and other services write the log.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. D/BootCompletedIntentReceiver( 576): android.intent.action.BOOT_COMPLETED
  3.  
  4. ...
  5.  
  6. D/BootCompletedIntentReceiver( 576): BackgroundService started
  7.  
  8. ...
  9.  
  10. D/ActivityThread( 576): Creating service aexp.persistentapp.BackgroundService
  11.  
  12. D/BackgroundService( 576): onStart
  13.  
  14. I/ActivityManager( 510): Stopping service: {aexp.persistentapp/aexp.persistentapp.BackgroundService}
  15.  
  16. D/ActivityThread( 576): Stopping service aexp.persistentapp.BackgroundService@400bcb18
  17.  
  18. D/BackgroundService( 576): onDestroy
  19.  
  20. ...
  21.  
  22. D/BackgroundService( 576): Counter: 10
  23.  
  24. ...
  25.  
  26. D/BackgroundService( 576): Counter: 9
  27.  
  28. D/BackgroundService( 576): Counter: 8
  29.  
  30. D/BackgroundService( 576): Counter: 7
  31.  
  32. ....
  33.  
  34. D/BackgroundService( 576): Counter: 6
  35.  
  36. D/BackgroundService( 576): Counter: 5
  37.  
  38. ...
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
paller
Developer
Developer
 
Posts: 29
Joined: Mon Dec 31, 2007 2:33 am
Location: London

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests