Introduction
Android services stand apart from the typical Android application which includes an Activity and a rich user interaction. Services on the Android platform utilize background processing and communicate to listeners by firing Intents, updating content providers, and/or triggering notifications. The Activity that started the service can be inactive or even closed and the service will continue to run.
A second form of an Android Service provides an interface to a remote object. Both extend the Service class and override specific functions to provide the desired functionality. This article will be covering the first form of a service.
Services normally can be stopped, started, and controlled from other applications. These applications include other Services or Activities. Services also receive higher priority than any inactive or invisible Activities. This means your Service will be less likely to be prematurely stopped by the resource manager.
Create the Service
To create an Android Service you must create a class which extends android.app.Service. To provide basic control and functionality you can override the onCreate(), onStart(), and onDestroy() methods. You must also add the service to the application manifest file. You can do that using the Eclipse ADT Plugin or by adding a Service block to the AndroidManifest.xml file.
Here is the skeleton service I will be fleshing out in this article:
package com.demo.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class RSSService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Here is the ApplicationManifest.xml entry for the example service in this article:
<
service
android:permission
=
"android.permission.INTERNET"
android:name
=
".RSSService"
android:enabled
=
"true"
></
service
>
0 comments:
Post a Comment