MainActivity

...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.example.boostcoursepractice;
 
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    Button serviceStartBtn ;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        serviceStartBtn = (Button) findViewById(R.id.btn_start_service);
        editText = (EditText) findViewById(R.id.editText);
 
        serviceStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = editText.getText().toString();
 
                Intent intent = new Intent(getApplicationContext(), MyService.class);
                intent.putExtra("command""show");
                intent.putExtra("name", name) ;
                startService(intent);
 
            }
        });
 
        Intent intent = getIntent() ;
        processCommand(intent) ;
    }
 
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
 
        processCommand(intent) ;
    }
 
    private void processCommand(Intent intent)
    {
        if(intent != null)
        {
            String command = intent.getStringExtra("command") ;
            String name = intent.getStringExtra("name");
            Toast.makeText(getApplicationContext(), "서비스로부터 전달 받은 데이터 : " + command + ", " + name, Toast.LENGTH_LONG).show();
 
        }
    }
}
 
cs

 

MyService

...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.example.boostcoursepractice;
 
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
 
public class MyService extends Service {
 
    private static final String TAG = "MyService";
 
    public MyService() {
 
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        Toast.makeText(getApplicationContext(), "onCreate() 호출됨.", Toast.LENGTH_LONG).show();
       // Log.d(TAG, "onCreate() 호출됨.");
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //log.d(TAG, "onStartCommand() 호출됨.");
        Toast.makeText(getApplicationContext(), "onStartCommand() 호출됨.", Toast.LENGTH_LONG).show();
 
        //if(intent == null) return Service.START_STICKY ;
        //else processCommand(intent);
        processCommand(intent);
 
        return super.onStartCommand(intent, flags, startId);
    }
 
    private void processCommand(Intent intent)
    {
        String command = "" ;
        String name = "";
        if(intent != null)
        {
            command = intent.getStringExtra("command");
            name = intent.getStringExtra("name");
            //Log.d(TAG, "전달 받은 데이터 : " + command + ", "  +name);
 
        }
 
        Toast.makeText(getApplicationContext(), "전달 받은 데이터 : " + command + ", "  +name, Toast.LENGTH_LONG).show();
 
        try{
            Thread.sleep(5000);
        }
        catch (Exception e){}
 
        Intent showIntent = new Intent(getApplicationContext(), MainActivity.class);
        showIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
        showIntent.putExtra("command","show");
        showIntent.putExtra("name", name + "from service");
        startActivity(showIntent);
    };
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        //Log.d(TAG, "onDestroy() 호출됨.");
        Toast.makeText(getApplicationContext(), "onDestroy() 호출됨.", Toast.LENGTH_LONG).show();
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
 
 
}
 
cs

 

서비스는 처음 생성되면 onCreate() processCommand() 순으로 호출 된다. 서비스는 한번 실행이 되면 없어 지지 않고 그 상태에서 다시 실행되도 onCreate()는 실행되지 않고 processCommand()만실행 된다. 또한 어플리케이션을 완전히 종료 되도 자동으로 시스템이 서비스를 다시 실행 시켜준다 이때는 다시 onCreate() processCommand() 순으로 실행된다. 

 

추가적으로 메인 액티비티에

 

protected void onNewIntent(Intent intent)

 

이런 콜백함수가 있는데 이것은 기존에 존재하는 액티비티가 있는데 인텐트의 플레그설정에 따라 다시 그 액티비티가 재사용되면  onNewIntent(Intent intent)가 실행된다. 

+ Recent posts