반응형

코드

MainActivity

package com.shannon.servicetset;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.startService(new Intent(this, MyService.class));
        finish();
    }
}

 

MyService.java

package com.shannon.servicetset;

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

import static android.widget.Toast.makeText;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public int onStartCommand (Intent intent,
                               int flags,
                               int startId) {
        makeText(getApplicationContext(), "서비스만 시작 i5i5.tistory.com", Toast.LENGTH_SHORT).show();
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

 

AndroidManifext.xml

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

    <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/Theme.Servicetset">

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
        </service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

activity_main.xml

 

android:theme="@android:style/Theme.Translucent.NoTitleBar"

는 타이틀바 삭제하는 코드.

<?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:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <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>

 

 

결과

액티비티가, 서비스만 실행 후, 바로 액티비티가 종료된다.

서비스는 시작할 떄, toast 메시지를 출력하고 사라진다.

반응형