WhatTime : アクティブになった時のみ時刻表示

MainActivity
package app.lightbox.winofsql.jp.whattime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


public class MainActivity extends Activity {

	// *************************************
	// 自動作成( onCreate )
	// *************************************
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Log.i("lightbox", "開始");

	}

	// *************************************
	// CTRL + O から追加( onStart, onStop, onPause )
	// *************************************
	@Override
	protected void onStart() {
		super.onStart();

		TimeZone tz = TimeZone.getTimeZone("Asia/Tokyo");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH'時'mm'分'ss'秒'");
		sdf.setTimeZone(tz);
		String time = sdf.format(date);
		Log.i("lightbox", time);

		TextView tv = (TextView) this.findViewById(R.id.text);
		tv.setText(time);

	}

	@Override
	protected void onStop() {
		super.onStop();

		Log.i("lightbox", "画面が切り替わりました");

	}

	@Override
	protected void onPause() {
		super.onPause();

		Log.i("lightbox", "画面が切り替わろうとしています");

	}

	// *************************************
	// 自動作成( onCreateOptionsMenu )
	// *************************************
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	// *************************************
	// 自動作成( onOptionsItemSelected )
	// *************************************
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == R.id.action_settings) {

			Log.i("lightbox", "メニュー");

			Intent intent = new Intent(MainActivity.this, NextPage.class);
			startActivity(intent);

			return true;
		}

		return super.onOptionsItemSelected(item);
	}
}

クラス java.util.TimeZone
クラス java.util.Date
クラス java.text.SimpleDateFormat


画面 ( activity_main.xml )
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="40dp"
        android:textColor="#ff000000"
        android:id="@+id/text"/>

</RelativeLayout>


次画面 ( NextPage )

次画面には、Hello World が表示されます
package app.lightbox.winofsql.jp.whattime;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by lightbox on 2015/05/22.
 */
public class NextPage extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

	}

}


AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.lightbox.winofsql.jp.whattime" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".NextPage">
        </activity>
    </application>

</manifest>


タイマーを使って時刻を変化させる
	private Timer timer = null;
	private Handler handler = null;

	// *************************************
	// CTRL + O から追加( onStart, onStop, onPause )
	// *************************************
	@Override
	protected void onStart() {
		super.onStart();

		timer = new Timer();
		handler = new Handler();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				handler.post(new Runnable() {
					@Override
					public void run() {
						Log.i("lightbox", "タイマー実行中です");

						TimeZone tz = TimeZone.getTimeZone("Asia/Tokyo");
						Date date = new Date();
						SimpleDateFormat sdf = new SimpleDateFormat("HH'時'mm'分'ss'秒'");
						sdf.setTimeZone(tz);
						TextView tv = (TextView) MainActivity.this.findViewById(R.id.text);
						tv.setText(sdf.format(date));

					}
				});
			}
		}, 0, 1000);

	}

	@Override
	protected void onStop() {
		super.onStop();

		timer.cancel();
		timer.purge();

		Log.i("lightbox", "画面が切り替わりました");

	}
クラス java.util.Timer
クラス java.util.TimerTask

クラス android.os.Handler
インタフェース java.lang.Runnable