Vinculando um Service Android a uma Activity

Introdução ao Vinculamento de Service

No Android, um Service pode ser vinculado a uma Activity para permitir interação direta, como acessar métodos do Service a partir da Activity. Ao contrário de iniciar um Service com startService(), o vinculamento usa bindService(), criando uma conexão que permite a obtenção de uma instância Binder.

Gerenciamento do Ciclo de Vida

É importante etnender o ciclo de vida ao combinar os métodos de início e vinculação:

  • Se apenas startService() e stopService() forem chamados, onDestroy() é execuatdo.
  • Se apenas bindService() e unbindService() forem chamados, onDestroy() também é executado.
  • Ao usar ambos startService() e bindService(), é necessário chamar tanto stopService() quanto unbindService() para que onDestroy() seja acionado.

Exemplo de Código

Primeiro, o layout da Activity com botões para controlar o Service:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_iniciar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Iniciar Service" />

    <Button
        android:id="@+id/btn_parar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Parar Service" />

    <Button
        android:id="@+id/btn_vincular"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Vincular Service" />

    <Button
        android:id="@+id/btn_desvincular"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Desvincular Service" />

</LinearLayout>

A classe Service com um Binder interno:

package com.example.servicedemo;

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

public class AudioPlaybackService extends Service {

    private final AudioBinder binder = new AudioBinder();

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

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public class AudioBinder extends Binder {
        public void startPlayback() {
            Toast.makeText(AudioPlaybackService.this, "Reproduzindo áudio em segundo plano", Toast.LENGTH_SHORT).show();
        }

        public int fetchPlaybackStatus() {
            Toast.makeText(AudioPlaybackService.this, "Progresso: 75%", Toast.LENGTH_SHORT).show();
            return 75;
        }
    }
}

A Activity pricnipal que gerencia a vinculação:

package com.example.servicedemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    private AudioPlaybackService.AudioBinder audioBinder;
    private boolean isServiceBound = false;

    private ServiceConnection serviceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            audioBinder = (AudioPlaybackService.AudioBinder) service;
            audioBinder.startPlayback();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            audioBinder = null;
        }
    };

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

    public void handleClick(View view) {
        Intent serviceIntent = new Intent(this, AudioPlaybackService.class);
        switch (view.getId()) {
            case R.id.btn_iniciar:
                startService(serviceIntent);
                break;
            case R.id.btn_parar:
                stopService(serviceIntent);
                break;
            case R.id.btn_vincular:
                isServiceBound = bindService(serviceIntent, serviceConn, BIND_AUTO_CREATE);
                if (isServiceBound) {
                    Toast.makeText(this, "Service vinculado", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_desvincular:
                if (isServiceBound) {
                    unbindService(serviceConn);
                    isServiceBound = false;
                    Toast.makeText(this, "Service desvinculado", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Service não vinculado", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}

Ao vincular a Activity ao Service, é possível chamar métodos como startPlayback() diretamente através do Binder, garantindo comunicação eficiente.

Tags: android Service Activity ServiceConnection Binder

Publicado em 6-24 01:31