MainActivity
01.
package
app.lightbox.winofsql.jp.whattime;
02.
03.
import
android.app.Activity;
04.
import
android.content.Intent;
05.
import
android.os.Bundle;
06.
import
android.util.Log;
07.
import
android.view.Menu;
08.
import
android.view.MenuItem;
09.
import
android.widget.TextView;
10.
11.
import
java.text.SimpleDateFormat;
12.
import
java.util.Date;
13.
import
java.util.TimeZone;
14.
15.
16.
public
class
MainActivity
extends
Activity {
17.
18.
// *************************************
19.
// 自動作成( onCreate )
20.
// *************************************
21.
@Override
22.
protected
void
onCreate(Bundle savedInstanceState) {
23.
super
.onCreate(savedInstanceState);
24.
setContentView(R.layout.activity_main);
25.
26.
Log.i(
"lightbox"
,
"開始"
);
27.
28.
}
29.
30.
// *************************************
31.
// CTRL + O から追加( onStart, onStop, onPause )
32.
// *************************************
33.
@Override
34.
protected
void
onStart() {
35.
super
.onStart();
36.
37.
TimeZone tz = TimeZone.getTimeZone(
"Asia/Tokyo"
);
38.
Date date =
new
Date();
39.
SimpleDateFormat sdf =
new
SimpleDateFormat(
"HH'時'mm'分'ss'秒'"
);
40.
sdf.setTimeZone(tz);
41.
String time = sdf.format(date);
42.
Log.i(
"lightbox"
, time);
43.
44.
TextView tv = (TextView)
this
.findViewById(R.id.text);
45.
tv.setText(time);
46.
47.
}
48.
49.
@Override
50.
protected
void
onStop() {
51.
super
.onStop();
52.
53.
Log.i(
"lightbox"
,
"画面が切り替わりました"
);
54.
55.
}
56.
57.
@Override
58.
protected
void
onPause() {
59.
super
.onPause();
60.
61.
Log.i(
"lightbox"
,
"画面が切り替わろうとしています"
);
62.
63.
}
64.
65.
// *************************************
66.
// 自動作成( onCreateOptionsMenu )
67.
// *************************************
68.
@Override
69.
public
boolean
onCreateOptionsMenu(Menu menu) {
70.
getMenuInflater().inflate(R.menu.menu_main, menu);
71.
return
true
;
72.
}
73.
74.
// *************************************
75.
// 自動作成( onOptionsItemSelected )
76.
// *************************************
77.
@Override
78.
public
boolean
onOptionsItemSelected(MenuItem item) {
79.
int
id = item.getItemId();
80.
81.
if
(id == R.id.action_settings) {
82.
83.
Log.i(
"lightbox"
,
"メニュー"
);
84.
85.
Intent intent =
new
Intent(MainActivity.
this
, NextPage.
class
);
86.
startActivity(intent);
87.
88.
return
true
;
89.
}
90.
91.
return
super
.onOptionsItemSelected(item);
92.
}
93.
}
クラス java.util.TimeZone クラス java.util.Date クラス java.text.SimpleDateFormat 画面 ( activity_main.xml )
01.
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
02.
xmlns:tools
=
"http://schemas.android.com/tools"
03.
android:layout_width
=
"match_parent"
04.
android:layout_height
=
"match_parent"
05.
android:paddingLeft
=
"@dimen/activity_horizontal_margin"
06.
android:paddingRight
=
"@dimen/activity_horizontal_margin"
07.
android:paddingTop
=
"@dimen/activity_vertical_margin"
08.
android:paddingBottom
=
"@dimen/activity_vertical_margin"
09.
tools:context
=
".MainActivity"
>
10.
11.
<
TextView
12.
android:text
=
"@string/hello_world"
13.
android:layout_width
=
"wrap_content"
14.
android:layout_height
=
"wrap_content"
15.
android:layout_centerInParent
=
"true"
16.
android:textSize
=
"40dp"
17.
android:textColor
=
"#ff000000"
18.
android:id
=
"@+id/text"
/>
19.
20.
</
RelativeLayout
>
次画面 ( NextPage ) 次画面には、Hello World が表示されます
01.
package
app.lightbox.winofsql.jp.whattime;
02.
03.
import
android.app.Activity;
04.
import
android.os.Bundle;
05.
06.
/**
07.
* Created by lightbox on 2015/05/22.
08.
*/
09.
public
class
NextPage
extends
Activity {
10.
@Override
11.
protected
void
onCreate(Bundle savedInstanceState) {
12.
super
.onCreate(savedInstanceState);
13.
14.
setContentView(R.layout.activity_main);
15.
16.
}
17.
18.
}
AndroidManifest
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
03.
package
=
"app.lightbox.winofsql.jp.whattime"
>
04.
05.
<
application
06.
android:allowBackup
=
"true"
07.
android:icon
=
"@mipmap/ic_launcher"
08.
android:label
=
"@string/app_name"
09.
android:theme
=
"@style/AppTheme"
>
10.
<
activity
11.
android:name
=
".MainActivity"
12.
android:label
=
"@string/app_name"
>
13.
<
intent-filter
>
14.
<
action
android:name
=
"android.intent.action.MAIN"
/>
15.
16.
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
17.
</
intent-filter
>
18.
</
activity
>
19.
<
activity
android:name
=
".NextPage"
>
20.
</
activity
>
21.
</
application
>
22.
23.
</
manifest
>
タイマーを使って時刻を変化させる
01.
private
Timer timer =
null
;
02.
private
Handler handler =
null
;
03.
04.
// *************************************
05.
// CTRL + O から追加( onStart, onStop, onPause )
06.
// *************************************
07.
@Override
08.
protected
void
onStart() {
09.
super
.onStart();
10.
11.
timer =
new
Timer();
12.
handler =
new
Handler();
13.
timer.schedule(
new
TimerTask() {
14.
@Override
15.
public
void
run() {
16.
handler.post(
new
Runnable() {
17.
@Override
18.
public
void
run() {
19.
Log.i(
"lightbox"
,
"タイマー実行中です"
);
20.
21.
TimeZone tz = TimeZone.getTimeZone(
"Asia/Tokyo"
);
22.
Date date =
new
Date();
23.
SimpleDateFormat sdf =
new
SimpleDateFormat(
"HH'時'mm'分'ss'秒'"
);
24.
sdf.setTimeZone(tz);
25.
TextView tv = (TextView) MainActivity.
this
.findViewById(R.id.text);
26.
tv.setText(sdf.format(date));
27.
28.
}
29.
});
30.
}
31.
},
0
,
1000
);
32.
33.
}
34.
35.
@Override
36.
protected
void
onStop() {
37.
super
.onStop();
38.
39.
timer.cancel();
40.
timer.purge();
41.
42.
Log.i(
"lightbox"
,
"画面が切り替わりました"
);
43.
44.
}
クラス java.util.Timer クラス java.util.TimerTask クラス android.os.Handler インタフェース java.lang.Runnable