Lab - 3b - Android Service
Lab - 3b - Android Service
Sem. II 2019/20
Lab 3b: Android Service
Introduction
Service is a process, but the special thing is about the service is it doesn’t need user
interaction and it runs on background. E.g., like playing music in background. It is a long
running process and it does not need user interaction.
1
1. Android service
To create a service, we need to create a normal class extending the class Service. And
need to override the following methods as shown in Figure 1:
onStartCommand()
The method is invoked when the service is started using
the startService() method. The method startService() can be called
from any activity and it will request the service to start.
onBind()
The method is called when to bind the service with an activity. The service can result
back something to the activity after binding. But if do not want to bind the service
with activity, just return null on this method.
onCreate()
This method is called when the service is created.
onDestroy()
When the service is no longer used and destroyed this method is called by the system.
2. Defining it on Manifest
Put the above code inside the application tag for every service.
2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:orientation="vertical"
tools:ignore="RtlCompat">
<Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start Service" />
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop Service" />
</LinearLayout>
</RelativeLayout>
3
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
//Button objects
private Button buttonStart;
private Button buttonStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View view) {
if(view == buttonStart){
//Start the service here
} else if (view == buttonStop) {
//Stop the service here
}
}
}
import android.app.Service;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.provider.Settings;
4
import android.support.annotation.Nullable;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Getting system default ringtone
player.setLooping(true);
@Override
public void onDestroy() {
super.onDestroy();
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
5
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</application>
</manifest>
Add toast functions to your code, so that everytime you click on the Start Service and Stop
Service buttons, a message will appear denoting either “Your service has started” or “Your
service has stopped”.
6
Additional exercise: