To declare your own service, it must be defined in the manifest as a service and the implementing class must extend Service or one of its subclasses. A service will not automatically run in its own thread without the process attribute. They by default run in the main thread of the hosting process. To define the process, in the manifest set the process under the application tab. An example is :my_service. The colon at the start tells android that the process is private to declaring applications. Otherwise the process would be globally accessible.
An Activity can start the Service by calling the startService() method and stop the service via the stopService() method. After a service is started the onCreate() method is called and later the onStartCommand gets invoked with the Intent.
Starting services automatically
To start services automatically after boot involves registering a BroadcastReceiver to the Android android.intent.action.RECEIVE_BOOT_COMPLETED system event. This requires the RECEIVE_BOOT_COMPLETED permission. In the broadcast receiver class, override the onReceive() method and call the service like:
Intent service = new Intent(context, MyService.class);
context.startService(service);
Related Information
Android NetworkingAndroid Services
No comments:
Post a Comment