Louie NRT Story

[안드로이드] IntentService 만들기 본문

전기차충전기

[안드로이드] IntentService 만들기

hyeok0724.kim@gmail.com 2021. 12. 12. 16:30
반응형

작성일: 21년 12월 12일

PS. 충전기 개발할때 UI와는 별도로 무거운 작업을 할때 수행하면 좋음. 예를 들어 서버로부터 다운로드를 한다거나 데이터들을 분석하고 서버에 데이터를 업로드 한다는 등의 이벤트성 작업에 좋을 것으로 판단됨

 

 

Index

1. 소스코드

2. 동작확인

3. IntentService와 Service 차이점

 

 

1. 소스코드

 1) IntentService 코드를 생성함

 

2) MyIntetnService

  - IntentService 코드를 만듬

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
       for (int i = 0; i < 5; i++){
           try{
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               Thread.currentThread().interrupt();
           }
           Log.d("MyIntentService", "인텐트 동작 중 " + i);
       }
    }
}

 

3) MainActivity

 - IntentService를 실행 시킬 수 있는 코드를 입력함

 - StartIntentService()가 IntentService 동작시키는 코드임

public class MainActivity extends AppCompatActivity {

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

    public void startService(View view) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void stopService(View view) {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

    public void StartIntentService(View view) {
        Intent intent = new Intent(this, MyIntentService.class);
        startService(intent);
    }
}

 

4) main_activity.xml

 - "인텐드서비스 시작" 버튼을 누르면 IntentService 호출함

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startService"
        android:text="Service Start"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stopService"
        android:text="Service Stop"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="StartIntentService"
        android:text="인텐드서비스 시작"/>
        
</LinearLayout>

 

 

2. 동작 확인

 - "인텐드서비스 시작" 버튼을 여러번 눌러도 순서대로 0 -> 1 -> 2 -> 3 -> 4 순서대로 계속 나오고 있음을 알 수 있음

 

 

3. IntentService와 Service 차이점

 - 둘의 가장 큰 차이점은 별도의 Queue를 가지고 있느냐 차이점인 것으로 생각됨

 - IntentService는 Intent를 받으면 IntentQueue에 넣어 받은 순서대로 작업을 수행함

 - IntentService는 작업이 완료되면 별도의 코드 없이 스스로 종료하지만 Service의 경우 별도로 종료가 되도록 작업을 해주어야함

출처: https://beck999.tistory.com/4

 

 

Referece:

https://youtu.be/8PJ12A1C35M

https://beck999.tistory.com/4

 

Service 와 IntentService 차이점

Service : UI Thread에서 동작 (The Service runs in background but it runs on the Main Thread of the application) IntentService :  Worker 스레드에서 동작 (The IntentService runs on a separate worker..

beck999.tistory.com

https://hijjang2.tistory.com/460

 

[Android] 서비스 2가지 방법- Service와 IntentService의 차이점

(참고) IntentService는 1.자동으로 종료가 되고, 2.스레드를 사용하지 않아도 된다. 왜냐하면 onHandleIntent()는 "백그라운드 스레드"로 동작을 하게 됩니다 service는 1.종료가 되지 않는다!!!! 1. 언제쓰이

hijjang2.tistory.com

https://m.blog.naver.com/alsrb524/221820800178

 

5. Intents and Broadcasting & Services

1.Intent란? 1) android 4대 컴포넌트(Ac, Ser, Broad, Cont) 간에 작업수행을 위한 정보전달역할 2) ...

blog.naver.com

 

반응형
Comments