package net.ddns.mahaljsp.ch15_05_handlerservice;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
 * Created by Thomas Wu on 2016/12/13.
 */
public class MainActivity extends AppCompatActivity {
    TextView txtTime;
    Intent intent;
    static Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        processViews();
        processControllers();
    }
    public void processViews(){
        txtTime=(TextView)findViewById(R.id.txtTime);
        intent=new Intent(this, CountService.class);
    }
    public void processControllers(){
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch(msg.what){
                    case 0:
                        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                        sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
                        txtTime.setText(sdf.format(new Date()));
                        break;
                }
                super.handleMessage(msg);
            }
        };
        startService(intent);
    }
    @Override
    protected void onDestroy() {
        stopService(intent);
        super.onDestroy();
    }
}
