본문 바로가기
매일코딩/안드로이드

[안드로이드 기초] 서비스

by 인생여희 2017. 8. 17.
반응형

서비스


#개념

서비스는 화면이 없는 상태에서 백그라운드로 실행됨

서비스는 프로세스가 종료되어도 시스템에서 자동으로 재시작함료되어도 시스템에서 자동으로 재시작함




#main.java 디자인


#main.java 소스

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
package me.happygate.myapplication222;
 
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
 
    EditText editText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
         editText =(EditText)findViewById(R.id.editText);
 
        Button button =(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                String name = editText.getText().toString();
                
 
                //서비스에 데이터 전달하기
                 Intent intent = new Intent(getApplicationContext(),MyService.class);
                intent.putExtra("command","show");
                intent.putExtra("name",name);
                startService(intent);
            }
        });
        
        
        //서비스로부터 데이터 전달받기
        Intent passedIntent = getIntent();
        processCommand(passedIntent);
 
    }
 
 
    
//서비스로부터 데이터 전달받기
    @Override
    protected void onNewIntent(Intent intent) {
        processCommand(intent);
        super.onNewIntent(intent);
    }
 
    private void processCommand(Intent intent){
        if(intent != null){
            String command = intent.getStringExtra("command");
            String name = intent.getStringExtra("name");
 
            Toast.makeText(this,"서비스로 부터 전달받은 데이터:"+command+","+name,Toast.LENGTH_SHORT).show();
        }
 
 
    }
}
 
cs


#MyService.java 소스

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
80
81
82
83
84
85
86
87
88
89
package me.happygate.myapplication222;
 
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
 
 
public class MyService extends Service {
 
    private static final String TAG = "MyService";
 
    public MyService() {
    }
 
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        Log.d(TAG,"onCreate");
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"onStartCommand");
 
 
        //intent로 던진 데이터는 이곳에서 받는다.
 
        if(intent == null){
 
            //계속 서비스를 실행시켜달라
            return  Service.START_STICKY;
        }else{
 
            processCommand(intent);
 
        }
 
        return super.onStartCommand(intent, flags, startId);
 
    }
    
 
    protected void processCommand (Intent intent){
 
        String command=intent.getStringExtra("command");
        String name = intent.getStringExtra("name");
        Log.d(TAG,"전달받은 데이터:"+command+","+name);
 
 
 
 
        //!!!!!!서비스에서 엑티비티로 데이터 전달하기
        try{
            Thread.sleep(5000);
        }catch (Exception e){}
 
      Intent showIntent = new Intent(getApplicationContext(),MainActivity.class);
            showIntent.addFlags(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");
    }
 
 
 
 
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
 
cs


#모니터창


처음에는 onCreate -> onStartCommand 순으로 실행되고  이후에는 onStartCommand 만 실행


반응형

댓글