본문 바로가기

안드로이드 Android

[모바일프로그래밍] 10-2 서비스

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:layout_marginStart="141dp"
        android:layout_marginTop="74dp"
        android:layout_marginEnd="174dp"
        android:text="음악 서비스 예제"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="118dp"
        android:layout_marginTop="39dp"
        android:layout_marginEnd="163dp"
        android:text="배경음악으로 시작"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="157dp"
        android:layout_marginTop="69dp"
        android:layout_marginEnd="150dp"
        android:text="배경음악 중지"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>

MusicService.java

package com.example.myapplication;

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

public class MusicService extends Service {
    MediaPlayer myplayer;

    public IBinder onBind(Intent myintent) {
        return null;
    }

    public void onCreate() {
        myplayer = MediaPlayer.create(this, R.raw.m83_midnight_city);
        myplayer.setLooping(false);
    }

    public void onDestroy() {
        Toast.makeText(this, "MusicService가 중지되었음", Toast.LENGTH_SHORT).show();
        myplayer.stop();
    }

    public int onStartCommand(Intent myintent, int flags, int startId) {
        Toast.makeText(this, "MusicService가 시작됨", Toast.LENGTH_LONG).show();
        myplayer.start();
        return super.onStartCommand(myintent, flags, startId);
    }
}

MainActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    Button start, end;

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

        start = (Button) findViewById(R.id.button);
        end = (Button) findViewById(R.id.button2);

        start.setOnClickListener((View.OnClickListener) this);
        end.setOnClickListener((View.OnClickListener) this);
    }

    public void onClick(View src) {
        switch (src.getId()) {
            case R.id.button:
                startService(new Intent(this, MusicService.class));
                break;
            case R.id.button2:
                stopService(new Intent(this, MusicService.class));
                break;
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".MusicService"
            android:enabled="true" />
    </application>
</manifest>

앱을 실행시키면 바로 배경음악이 재생되어야 하는데 앱이 종료된다.