1. Trang chủ
  2. » Công Nghệ Thông Tin

Android Programing Bài 14: Example1 (Cont)

16 55 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 16
Dung lượng 75,06 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

BÀI 13: XÂY DỰNG ỨNG DỤNG DỰA TRÊN CÁC LỚP CỦA ANDROIDtiếp - Xây dựng được chương trình đồng hồ báo thức trên điện thoại di động ứng dụng dựa trên các lớp của Android Đây là ứng dụng báo

Trang 1

BÀI 13: XÂY DỰNG ỨNG DỤNG DỰA TRÊN CÁC LỚP CỦA ANDROID

(tiếp)

- Xây dựng được chương trình đồng hồ báo thức trên điện thoại di động ứng dụng dựa trên các lớp của Android

Đây là ứng dụng báo thức đơn giản nhằm mục đích làm quen với môi trường lập trình Android Ứng dụng này có chức năng chính là hẹn giờ báo thức, có thể báo thức 1 lần hoặc lặp lại ở các ngày tiếp theo Chương trình dùng nhạc chuông mặc định sẵn và chế độ rung để báo thức.

Giao diện chính của ứng dụng:

Hình 39: Giao diện chính của ứng dụng báo thức

Chúng ta có thể chọn giờ báo thức để báo thức 1 lần hoặc check vào “Lặp lại báo thức vào ngày sau” để báo thức cho các ngày tiếp theo Sau đó chọn Cài đặt báo

thức Chúng ta có thể hủy báo thức đã được cài đặt trước đó bằng cách nhấn vào nút

“Hủy Báo Thức”.

Giao diện đến giờ báo thức:

Trang 2

Hình 40: Giao diện đến giờ báo thức

Nhằm mục đích giúp cho người dùng tỉnh táo hơn, nên khi muốn tắt báo thức người dùng phải kéo thanh SeekBar sang đầu bên phải Source code:

1. /*

2. * Copyright (C) 2007 The Android Open Source Project

3. *

4. * Licensed under the Apache License, Version 2.0 (the "License");

5. * you may not use this file except in compliance with the License

6. * You may obtain a copy of the License at

7. *

8. * http://www.apache.org/licenses/LICENSE-2.0

Trang 3

9. *

10. * Unless required by applicable law or agreed to in writing, software

11. * distributed under the License is distributed on an "AS IS" BASIS,

12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied

13. * See the License for the specific language governing permissions and

14. * limitations under the License

16

17. package com.android.deskclock;

18

19. import android.app.Activity;

20. import android.app.AlertDialog;

21. import android.content.Context;

22. import android.content.DialogInterface;

23. import android.content.Intent;

24. import android.content.SharedPreferences;

25. import android.content.res.Configuration;

26. import android.database.Cursor;

27. import android.database.DataSetObserver;

28. import android.net.Uri;

29. import android.os.Bundle;

Trang 4

30. import android.os.Handler;

31. import android.provider.Settings;

32. import android.view.ContextMenu;

33. import android.view.ContextMenu.ContextMenuInfo;

34. import android.view.LayoutInflater;

35. import android.view.Menu;

36. import android.view.MenuItem;

37. import android.view.View;

38. import android.view.View.OnClickListener;

39. import android.view.View.OnCreateContextMenuListener;

40. import android.view.ViewGroup;

41. import android.widget.AdapterView;

42. import android.widget.AdapterView.AdapterContextMenuInfo;

43. import android.widget.AdapterView.OnItemClickListener;

44. import android.widget.Button;

45. import android.widget.CheckBox;

46. import android.widget.CursorAdapter;

47. import android.widget.ListView;

48. import android.widget.TextView;

49

50. import java.util.Calendar;

Trang 5

51. import java.text.DateFormatSymbols;

52

54. * AlarmClock application

56. public class AlarmClock extends Activity implements OnItemClickListener 57

58. final static String PREFERENCES = "AlarmClock";

59

60. /** Cap alarm count at this number */

61. final static int MAX_ALARM_COUNT = 12;

62

63. /** This must be false for production If true, turns on logging,

64. test code, etc */

65. final static boolean DEBUG = false;

66

67. private SharedPreferences mPrefs;

68. private LayoutInflater mFactory;

69. private ListView mAlarmsList;

70. private Cursor mCursor;

71

72. private String mAm, mPm;

Trang 6

74. private class AlarmTimeAdapter extends CursorAdapter

75. public AlarmTimeAdapter(Context context, Cursor cursor) {

76. super(context, cursor);

78

79. public View newView(Context context, Cursor cursor, ViewGroup parent) {

80. View ret = mFactory.inflate( layout.alarm_time, parent, false);

81

82. ((TextView) ret.findViewById( id.am)).setText(mAm);

83. ((TextView) ret.findViewById( id.pm)).setText(mPm);

84

85. DigitalClock digitalClock = (DigitalClock)ret.findViewById( id.digitalClock);

86. digitalClock.setLive(false);

87. if Log.LOGV) Log ("newView " cursor.getPosition());

88. return ret;

90

91. public void bindView(View view, Context context, Cursor cursor) {

92. final Alarm alarm = new Alarm(cursor);

93

94. CheckBox onButton = (CheckBox)view.findViewById( id.alarmButton);

Trang 7

95. onButton.setChecked(alarm.enabled);

96. onButton.setOnClickListener(new OnClickListener()

97. public void onClick(View v) {

98. boolean isChecked = ((CheckBox) v).isChecked();

99. Alarms.enableAlarm(AlarmClock.this, alarm.id,

100. isChecked);

101. if isChecked) {

102. SetAlarm.popAlarmSetToast(AlarmClock.this,

103. alarm.hour, alarm.minutes, alarm.daysOfWeek);

107

108. DigitalClock digitalClock =

109. (DigitalClock) view.findViewById( id.digitalClock); 110

111. // set the alarm text

112. final Calendar c = Calendar.getInstance();

113. c set(Calendar.HOUR_OF_DAY, alarm.hour);

114. c set(Calendar.MINUTE, alarm.minutes);

115. digitalClock.updateTime( );

Trang 8

117. // Set the repeat text or leave it blank if it does not repeat

118. TextView daysOfWeekView =

119. (TextView) digitalClock.findViewById( id.daysOfWeek);

120. final String daysOfWeekStr =

121. alarm.daysOfWeek.toString(AlarmClock.this, false);

122. if daysOfWeekStr != null && daysOfWeekStr.length() != ) {

123. daysOfWeekView.setText(daysOfWeekStr);

124. daysOfWeekView.setVisibility(View.VISIBLE);

126. daysOfWeekView.setVisibility(View.GONE);

128

129. // Display the label

130. TextView labelView =

131. (TextView) digitalClock.findViewById( id.label);

132. if alarm.label != null && alarm.label.length() != ) {

133. labelView.setText(alarm.label);

134. labelView.setVisibility(View.VISIBLE);

136. labelView.setVisibility(View.GONE);

Trang 9

137. }

140

141. @Override

142. public boolean onContextItemSelected(final MenuItem item) {

143. final AdapterContextMenuInfo info =

144. (AdapterContextMenuInfo) item.getMenuInfo();

145. final int id = (int) info.id;

146. switch item.getItemId())

147. case R.id.delete_alarm:

148. // Confirm that the alarm will be deleted

149. new AlertDialog.Builder(this)

150. setTitle(getString( string.delete_alarm))

151. setMessage(getString( string.delete_alarm_confirm))

152. setPositiveButton(android .string.ok,

153. new DialogInterface.OnClickListener()

154. public void onClick(DialogInterface d,

155. int w) {

156. Alarms.deleteAlarm(AlarmClock.this, id);

Trang 10

158. })

159. setNegativeButton(android .string.cancel, null)

160. show();

161. return true;

162

163. case R.id.enable_alarm:

164. final Cursor c = (Cursor) mAlarmsList.getAdapter()

165. getItem(info.position);

166. final Alarm alarm = new Alarm( );

167. Alarms.enableAlarm(this, alarm.id, !alarm.enabled);

168. if (!alarm.enabled) {

169. SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,

170. alarm.daysOfWeek);

172. return true;

173

174. default:

175. break;

177. return super.onContextItemSelected(item);

Trang 11

180. @Override

181. protected void onCreate(Bundle icicle) {

182. super.onCreate(icicle);

183

184. String[] ampm = new DateFormatSymbols().getAmPmStrings();

185. mAm = ampm[ ];

186. mPm = ampm[ ];

187

188. mFactory = LayoutInflater.from(this);

189. mPrefs = getSharedPreferences(PREFERENCES, 0);

190. mCursor = Alarms.getAlarmsCursor(getContentResolver());

191

192. updateLayout();

194

195. private void updateLayout()

196. setContentView( layout.alarm_clock);

197. mAlarmsList = (ListView) findViewById( id.alarms_list);

198. AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);

199. mAlarmsList.setAdapter(adapter);

200. mAlarmsList.setVerticalScrollBarEnabled(true);

Trang 12

201. mAlarmsList.setOnItemClickListener(this);

202. mAlarmsList.setOnCreateContextMenuListener(this);

203

204. final Button addAlarm = (Button) findViewById( id.add_alarm);

205. addAlarm.setOnClickListener(new View.OnClickListener()

206. public void onClick(View v) {

207. Uri uri = Alarms.addAlarm(getContentResolver());

208. // FIXME: scroll to new item?

209. String segment = uri.getPathSegments().get( );

210. int newId = Integer.parseInt(segment);

211. if Log.LOGV) {

212. Log ("In AlarmClock, new alarm id = " newId);

214. Intent intent =

215. new Intent(AlarmClock.this, SetAlarm.class);

216. intent.putExtra(Alarms.ALARM_ID, newId);

217. startActivity(intent);

220. // Update the enabled state of the "Add alarm" menu item depending on

221. // how many alarms are set

Trang 13

222. adapter.registerDataSetObserver(new DataSetObserver()

223. public void onChanged()

224. updateAddAlarmButton(addAlarm);

226. public void onInvalidate()

227. updateAddAlarmButton(addAlarm);

230

231. Button settings = (Button) findViewById( id.settings);

232. settings.setOnClickListener(new View.OnClickListener()

233. public void onClick(View v) {

234. startActivity(

235. new Intent(AlarmClock.this, SettingsActivity.class));

239

240. private void updateAddAlarmButton(Button b) {

241. b setEnabled(mAlarmsList.getAdapter().getCount() MAX_ALARM_COUNT);

Trang 14

244. @Override

245. protected void onDestroy()

246. super.onDestroy();

247. ToastMaster.cancelToast();

248. mCursor.deactivate();

250

251. @Override

252. public void onCreateContextMenu(ContextMenu menu, View view,

253. ContextMenuInfo menuInfo) {

254. // Inflate the menu from xml

255. getMenuInflater().inflate( menu.context_menu, menu);

256

257. // Use the current item to create a custom view for the header

258. final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

259. final Cursor c =

260. (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);

261. final Alarm alarm = new Alarm( );

262

263. // Construct the Calendar to compute the time

264. final Calendar cal = Calendar.getInstance();

Trang 15

265. cal.set(Calendar.HOUR_OF_DAY, alarm.hour);

266. cal.set(Calendar.MINUTE, alarm.minutes);

267. final String time = Alarms.formatTime(this, cal);

268

269. // Inflate the custom view and set each TextView's text

270. final View v = mFactory.inflate( layout.context_menu_header, null);

271. TextView textView = (TextView) v.findViewById( id.header_time);

272. textView.setText(time);

273. textView = (TextView) v.findViewById( id.header_label);

274. textView.setText(alarm.label);

275

276. // Set the custom view on the menu

277. menu.setHeaderView( );

278. // Change the text to "disable" if the alarm is already enabled

279. if alarm.enabled) {

280. menu.findItem( id.enable_alarm).setTitle( string.disable_alarm);

283

284. public void onItemClick(AdapterView parent, View v, int pos, long id) {

285. Intent intent = new Intent(this, SetAlarm.class);

Trang 16

286. intent.putExtra(Alarms.ALARM_ID, (int) id);

287. startActivity(intent);

Ngày đăng: 22/07/2019, 14:55

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w