본문 바로가기

Android

[7일차]서비스

서비스는 윈도우를 가지지 않는다. 



실제 호출은 메인스레드로 호출한다. 



서비스 실습.

new - Service 

intent service 내부에 쓰레드를 가지는 서비스 다음에 배운다. 


생성할 때 exported "true" 하면 다른앱에서 호출하겠다는거.

onBind

oncreate() 에서토스트를 띄우기.

onStart는 Deprecated


그래서 onStartCommand를 쓴다.  여기에 토스트 메세지


종료되면 onDestroy 호출. 여기에 토스트 메세지.


서비스 만들면 menifest에 등록이 되야된다. 


액티비티에서 


 - 버튼 1 추가 서비스 시작하는 버튼, 인텐트만들면된다. context안에 mainActivity.this 를 넘겨준다. 그리고 startService(intent)

 - 버튼 2 추가 서비스 스탑하는 버튼, 인텐트 만들어서, stopService(intent);로 전달

 - 버튼 3 추가 서비스를 종료하는 버튼, 똑같다. 


boolean isRunning = false

int count ;

new Thread(new Runnable(){


public void run(){

while(isRunning)

log

count++

try{

Thread.sleep(1000)

}



){

}.start();

}


})




서비스에서 

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();
return Service.START_NOT_STICKY;

}
이렇게 해줘야 서비스를 종료해도 다시 실행하지 않는다. 

서비스에 값을 전달할때 ?

액티비티에서 서비스 인텐트 선언할때,
intent.putExtra("여기다 데이터를 넘기는 거다. ")


jobScheduler

onStartJob
jobFinished
onStopJob

permission 을 줘야한다. 
exported true여야한다. 외부에서 실행해야 하기 때문에 

JobInfo를 만들어서 어떻게 실행할지를 담고있는 class이다. 



'Android' 카테고리의 다른 글

[8일차]글로벌리시버, 로컬리시버  (0) 2016.07.22
[7일차]receiver  (0) 2016.07.21
[7일차]프래그먼트, BackStack  (0) 2016.07.21
[7일차]  (0) 2016.07.21
[7일차]메모리, Context  (0) 2016.07.21