package net.ddns.mahaljsp.ch15_03_bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Thomas Wu(mahaljsp@gmail.com) on 2016/12/13.
 */

public class CountService extends Service {
    boolean flag=true;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new CountBinder();
    }
    public class CountBinder extends Binder{
        public CountService getService(){
            return CountService.this;
        }
    }
    public void startCount(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count=0;
                while(flag){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.d("Thomas", (count++)+"秒");
                }
            }
        }).start();
    }
    public void stopCount(){
        flag=false;
    }
}
