질문과 같이 아래 코드로 10초 단위로 일정작업을 수행하도록 해놨습니다.

바로 아래에 startService(intent)와 stopService(intent)로 제어하구요

아래로 내려가시면



call
  // SendService 서비스 시작
  Button btn_start = (Button)findViewById(R.id.btn_start);
  btn_start.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    Intent intent = new Intent(SendCordData.this, SendService.class);
    startService(intent);
   }

  });
  // SendService 서비스 종료
  Button btn_stop = (Button)findViewById(R.id.btn_stop);
  btn_stop.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    Intent intent = new Intent(SendCordData.this, SendService.class);
    stopService(intent);
   }
  });

stop, start 되면서 Service가 백그라운드로 수행을하고,

handler.postDelay로 일정시간(10초)단위로 반복 수행합니다.

 stopTask 변수로 서비스를 계속 할지 안할지를 구분하는데요, 생각대로 멈추지를 않네요ㅠ

오류나서 어플이 죽어도 계속이 수행되고 그래요..ㄷㄷㄷ

stopTask의 설정쪽이 이상한것같은데 고수님들의 도움을 요청합니다! 



Service
public class SendService extends Service {
 final Handler mHandler = new Handler();
 boolean stopTask = true;

 public void onCreate() {
  super.onCreate();
  Toast.makeText(this, "Job Start", 0).show();
  stopTask = false;
  startService();
 }

 private void startService() { 
  final Runnable r = new Runnable(){
   @Override
   public void run() {
    // 주기적으로 반복될 내용  
    if(!stopTask)mHandler.postDelayed(this, 10000);
   }
  };
  if(!stopTask)mHandler.postDelayed(r, 10000);
 }

 private void stopService(){
  stopTask = true;
 }
 
 public void onDestroy() {
  stopService();
  super.onDestroy();
  Toast.makeText(this, "Job End", 0).show();
 }

 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }
}