A Service is an application component used to perform long-running tasks in background. There are 2 types of Service:
Bound Service: A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
Unbound Service: This service is completely independent for the Activity that called it and there is no communication or interaction between them.
We create a simple ubounded service example:
Step 1: create new project
Step 2: create new Class with name DemoService
package com.tutorialspots.service; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Tutorialspots on 1/8/2016. */ public class DemoService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { Log.i("DemoService", "Service onBind"); return null; } @Override public void onCreate() { Log.i("DemoService", "Service onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("DemoService", "Service onStartCommand"); return Service.START_STICKY; } @Override public void onDestroy() { Log.i("DemoService", "Service onDestroy"); } }
Step 3: create 2 new buttons
In file res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.tutorialspots.service.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TUTORIALSPOTS.COM" android:textColor="#FF0000" android:textSize="30dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start service" android:id="@+id/startbtn" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop service" android:id="@+id/stopbtn" android:layout_below="@+id/startbtn" android:layout_centerHorizontal="true" /> </RelativeLayout>
Step 4: in file MainActivity.java
package com.tutorialspots.service; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button startbtn, stopbtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get buttons startbtn = (Button) findViewById(R.id.startbtn); stopbtn = (Button) findViewById(R.id.stopbtn); startbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startService(new Intent(MainActivity.this, DemoService.class)); } }); stopbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { stopService(new Intent(MainActivity.this, DemoService.class)); } }); } }
Explanation:
To start a service: use Context.startService() or bindService() method
To stop a service: stopSelf() or stopService().
Step 5: file AndroidManifest.xml
Declare the service:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutorialspots.service"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".DemoService" android:exported="false"/> </application> </manifest>
Explanation:
“android:exported” means that your service is available to only your application.
Step 6: run for test
As soon as you click button Start Service, you see in the log:
01-07 18:13:32.628 2572-2572/com.tutorialspots.service I/DemoService: Service onCreate 01-07 18:13:32.628 2572-2572/com.tutorialspots.service I/DemoService: Service onStartCommand
But if you click this button again, you will see a log
01-07 18:13:32.628 2572-2572/com.tutorialspots.service I/DemoService: Service onStartCommand
Go to the manager Apps, we can see (even you close the application, you still see this)
As soon as you click button Stop service, you see in the log:
01-07 18:17:38.649 2572-2572/com.tutorialspots.service I/DemoService: Service onDestroy