ShareIntroduction
To create a application to run in the background of other current activities, one needs to create a Service. The Service can run indefinitely (unbounded) or can run at the lifespan of the calling activity(bounded).
Here you can see the android service lifecycle:
Android Services
Please note that a Service has a different lifecycle than activities therefore have different methods. But to begin a service in the application a call to
startService() which envokes the service
onCreate() method and
onStart() beginning running the service.
context.startService() | ->onCreate() - >onStartCommand() [service running]Calling the applications
stopService() method to stop the service.
context.stopService() | ->onDestroy() [service stops]Something that we didn't use in this example is
bindService() which just calls the services
onCreate() method but does not call the
onStartCommand().
onBindService() is used to create persistance connection to the service.
context.onBindService() | ->onCreate() [service created]This
Services Demo is simple as it plays a audio file and by listening to click events of the buttons invokes the
MyService service.
ServicesDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
package com.example; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ServicesDemo extends Activity implements OnClickListener { private static final String TAG = "ServicesDemo"; Button buttonStart, buttonStop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart = (Button) findViewById(R.id.buttonStart); buttonStop = (Button) findViewById(R.id.buttonStop); buttonStart.setOnClickListener(this); buttonStop.setOnClickListener(this); } public void onClick(View src) { switch (src.getId()) { case R.id.buttonStart: Log.d(TAG, "onClick: starting srvice"); startService(new Intent(this, MyService.class)); break; case R.id.buttonStop: Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(this, MyService.class)); break; } } }
|
The custom
MyService extends
Service class and necessary to override various methods of its lifecycle ie
onCreate(), onStartCommand(), or onDestroy()MyService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
package com.example; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { private static final String TAG = "MyService"; MediaPlayer player; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.braincandy); player.setLooping(false); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); } }
|
Necessary to let the AndroidManifest file know about your service
<service android:enabled="true" android:name=".MyService" />AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServicesDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:enabled="true" android:name=".MyService" /> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
|
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Services Demo"
android:gravity="center" android:textSize="20sp"
android:padding="20dp"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonStart" android:text="Start">
</Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" android:id="@+id/buttonStop">
</Button>
</LinearLayout>
|
You can download the source code below.
Source:
AndroidCore
Source code files
| File | Description | SDK Version | File size | Last Modified |
ServicesDemo.zip | The source code for this example | Min API version: 4 | 28 Kb | 16/11/10 15:00 |