Code: Bound Services

In this video I’m talking about bound services in Android. When and why we need them, connecting and disconnecting to the service, and how to create a bound service.

MainActivity.java

package org.meicode.boundservices;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.TextView;

/**
 * Created By Meisam at meiCode.org
 */

public class MainActivity extends AppCompatActivity {

    private boolean isBound = false;
    private ExampleService service;

    private TextView textView;
    private DisplayRandomAsyncTask displayRandomAsyncTask;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            ExampleService.LocalBinder binder = (ExampleService.LocalBinder) iBinder;
            service = binder.getService();

            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            isBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        displayRandomAsyncTask = new DisplayRandomAsyncTask();
        displayRandomAsyncTask.execute();
    }

    @Override
    protected void onStart() {
        super.onStart();

        Intent intent = new Intent(this, ExampleService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (isBound && service != null) {
            unbindService(serviceConnection);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (null != displayRandomAsyncTask) {
            if (!displayRandomAsyncTask.isCancelled()) {
                displayRandomAsyncTask.cancel(true);
            }
        }
    }

    private class DisplayRandomAsyncTask extends AsyncTask<Void, Integer, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            for (int i=0; i<10; i++) {
                if (isBound && service != null) {
                    publishProgress(service.getRandom());
                }

                SystemClock.sleep(1000);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

            textView.setText(String.valueOf(values[0]));
        }
    }
}

ExampleService.java

package org.meicode.boundservices;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

import java.util.Random;

/**
 * Created by Meisam at meiCode.org
 */

public class ExampleService extends Service {

    private IBinder binder = new LocalBinder();
    private Random random = new Random();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class LocalBinder extends Binder {
        ExampleService getService() {
            return ExampleService.this;
        }
    }

    public int getRandom() {
        return random.nextInt();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

<resources>
    <string name="app_name">Bound Services</string>
    <string name="service_desc">This service downloads stuff</string>
</resources>

Subscribe To Our Newsletter
Get updates and learn from the best

Leave a Reply

Your email address will not be published. Required fields are marked *

Andriod Course

Table of Contents