Android Services
An Android service is a component that is used to carry out background tasks like music playback, network transaction management, communicating with content providers, etc.
It does not have any user interface, Even if the application is destroyed, the service continues to execute in the background.
Type of Android Services :
Foreground Services: Foreground Services are those that inform the user of their continuing operations. Users can interact with the service by responding to notifications about a task that is in progress. The user can monitor download progress and pause and resume the process, for example when downloading a file.
Background Services: Background services don’t need any input from the user. These services are invisible to users and do not inform them of ongoing background work. This service includes operations like synchronizing data on a timetable or storing data.
Bound Services: The components of the programme, such as the activity, can bind themselves to this kind of Android service. As long as any application component is bound to a bound service, it will carry out its intended function. At any given time, more than one component may bind to a service. The bindService() method is used to connect an application component with a service.
Lifecycle of Service:
Started Service: When a component (such an activity) calls the startService() function, a service is launched and now runs continuously in the background. The stopService() method ends it. By using the stopSelf() method, the service can terminate itself.
Bound Service: When another component calls the bindService() function, a service is bound. By using the unbindService() method, the client can unbind the service.
1. create a new project name as Services.
2. Modify strings.xml file.
1 2 3 4 5 6 7 8 |
<resources> <string name="app_name">Service</string> <string name="start">Start Service</string> <string name="stop">Stop Service</string> <string name="next">Next</string> </resources> |
3. Write the code in activity_main.xml.
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 |
<?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"> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="170dp" android:textAlignment="center" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textColor="@android:color/holo_green_dark" android:textSize="36sp" android:textStyle="bold" /> <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="74dp" android:text="Start Service" /> <Button android:id="@+id/stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Stop Service" /> </RelativeLayout> |
4. Create a service class, this class extend the Service class, The callback methods are used to initiate and destroy the services. To play music, the MediaPlayer object is used, class name as 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.example.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.provider.Settings; import androidx.annotation.Nullable; public class MyService extends Service { private MediaPlayer player; @Override // execution of service will start // on calling this method public int onStartCommand(Intent intent, int flags, int startId) { // creating a media player which // will play the audio of Default // ringtone in android device player = MediaPlayer.create( this, Settings.System.DEFAULT_RINGTONE_URI ); // providing the boolean // value as true to play // the audio on loop player.setLooping( true ); // starting the process player.start(); // returns the status // of the program return START_STICKY; } @Override // execution of the service will // stop on calling this method public void onDestroy() { super.onDestroy(); // stopping the process player.stop(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } |
5. Now in the main MainActivity file perform the action on the button in MainActivity.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 43 44 45 46 47 48 49 50 51 52 53 |
package com.example.service; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button start, stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // assigning ID of startButton // to the object start start = (Button) findViewById( R.id.startButton ); // assigning ID of stopButton // to the object stop stop = (Button) findViewById( R.id.stopButton ); // declaring listeners for the // buttons to make them respond // correctly according to the process start.setOnClickListener((View.OnClickListener) this); stop.setOnClickListener((View.OnClickListener) this); } public void onClick(View view) { // process to be performed // if start button is clicked if(view == start){ // starting the service startService(new Intent( this, Next.class ) ); } // process to be performed // if stop button is clicked else if (view == stop){ // stopping the service stopService(new Intent( this, Next.class ) ); } } } |