DeskClock分析文档
用到了newView和bindView,通过前面的介绍,我们知道这两个方法的功能是:当列表排满时,第一次调用newView,之后只调用bindView就可以了。 private class AlarmTimeAdapter extends CursorAdapter {
public AlarmTimeAdapter(Context context, Cursor cursor) { super(context, cursor); Log.i(\ }
public View newView(Context context, Cursor cursor, ViewGroup parent) { View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
DigitalClock digitalClock =
(DigitalClock) ret.findViewById(R.id.digitalClock); digitalClock.setLive(false); Log.i(\ return ret; }
public void bindView(View view, Context context, Cursor cursor) { Log.i(\
final Alarm alarm = new Alarm(cursor);
View indicator = view.findViewById(R.id.indicator);
// Set the initial resource for the bar image. final ImageView barOnOff =
(ImageView) indicator.findViewById(R.id.bar_onoff); barOnOff.setImageResource(alarm.enabled ?
R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
// Set the initial state of the clock \ final CheckBox clockOnOff =
(CheckBox) indicator.findViewById(R.id.clock_onoff); clockOnOff.setChecked(alarm.enabled);
// Clicking outside the \ indicator.setOnClickListener(new OnClickListener() { public void onClick(View v) { clockOnOff.toggle();
updateIndicatorAndAlarm(clockOnOff.isChecked(), barOnOff, alarm); Log.i(\ } });
11
DeskClock分析文档
DigitalClock digitalClock =
(DigitalClock) view.findViewById(R.id.digitalClock);
// set the alarm text
final Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, alarm.hour); c.set(Calendar.MINUTE, alarm.minutes); digitalClock.updateTime(c);
digitalClock.setTypeface(Typeface.DEFAULT);
// Set the repeat text or leave it blank if it does not repeat. TextView daysOfWeekView =
(TextView) digitalClock.findViewById(R.id.daysOfWeek); final String daysOfWeekStr =
alarm.daysOfWeek.toString(AlarmClock.this, false); if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) { Log.i(\
daysOfWeekView.setText(daysOfWeekStr); daysOfWeekView.setVisibility(View.VISIBLE); } else {
Log.i(\
daysOfWeekView.setVisibility(View.GONE); }
// Display the label TextView labelView =
(TextView) view.findViewById(R.id.label); if (alarm.label != null && alarm.label.length() != 0) { Log.i(\
labelView.setText(alarm.label); labelView.setVisibility(View.VISIBLE); } else {Log.i(\
labelView.setVisibility(View.GONE); } } };
· 通过onCreateContextMenu方法,长按选中的闹钟,会显示对闹钟操作的选项 @Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { // Inflate the menu from xml.
getMenuInflater().inflate(R.menu.context_menu, menu);
12
DeskClock分析文档
// Use the current item to create a custom view for the header.
final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; final Cursor c =
(Cursor) mAlarmsList.getAdapter().getItem((int) info.position); final Alarm alarm = new Alarm(c);
// Construct the Calendar to compute the time. final Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, alarm.hour); cal.set(Calendar.MINUTE, alarm.minutes); final String time = Alarms.formatTime(this, cal);
// Inflate the custom view and set each TextView's text.
final View v = mFactory.inflate(R.layout.context_menu_header, null); TextView textView = (TextView) v.findViewById(R.id.header_time); textView.setText(time);
textView = (TextView) v.findViewById(R.id.header_label); textView.setText(alarm.label);
// Set the custom view on the menu. menu.setHeaderView(v); Log.i(\
// Change the text based on the state of the alarm. if (alarm.enabled) {Log.i(\
menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm); }
}
· 当点击menu时,通过重写onOptionsItemSelected,弹出菜单选项 @Override
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {
case R.id.menu_item_settings:Log.i(\ startActivity(new Intent(this, SettingsActivity.class)); return true;
case R.id.menu_item_desk_clock:Log.i(\ startActivity(new Intent(this, DeskClock.class)); return true;
case R.id.menu_item_add_alarm:Log.i(\ addNewAlarm(); return true;
default:Log.i(\ break;
13
DeskClock分析文档
}
return super.onOptionsItemSelected(item);
} //监听按钮
public void onItemClick(AdapterView parent, View v, int pos, long id) { Intent intent = new Intent(this, SetAlarm.class); intent.putExtra(Alarms.ALARM_ID, (int) id); startActivity(intent); Log.i(\ } }
? 闹铃后的操作
AlarmAlertFullScreen继承Activity,这是响铃界面。当系统发出广播时,通过BroadcastReceiver接受到广播,来触发响铃。 ? 打盹(snooze)
Snooze后,系统发出通知,提示下一次再响时间 private void snooze() {
// 不终止闹铃,则闹铃被禁用
if (!findViewById(R.id.snooze).isEnabled()) {Log.v(\ dismiss(false); return; }
final String snooze =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE); int snoozeMinutes = Integer.parseInt(snooze);
final long snoozeTime = System.currentTimeMillis() + (1000 * 60 * snoozeMinutes);
Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id, snoozeTime);
//得到显示时间打盹和更新的通知. final Calendar c = Calendar.getInstance(); c.setTimeInMillis(snoozeTime);
// (snoozed)添加标签.
String label = mAlarm.getLabelOrDefault(this);
label = getString(R.string.alarm_notify_snooze_label, label);
//通知用户已经snoozed警报。
14
DeskClock分析文档
Intent cancelSnooze = new Intent(this, AlarmReceiver.class); cancelSnooze.setAction(Alarms.CANCEL_SNOOZE); cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id); PendingIntent broadcast =
PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0); Log.v(\
NotificationManager nm = getNotificationManager();
Notification n = new Notification(R.drawable.stat_notify_alarm, label, 0);
n.setLatestEventInfo(this, label,
getString(R.string.alarm_notify_snooze_text, Alarms.formatTime(this, c)), broadcast); n.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT; nm.notify(mAlarm.id, n); Log.v(\
String displayTime = getString(R.string.alarm_alert_snooze_set, snoozeMinutes);
Log.v(displayTime);
// 显示toast。
Toast.makeText(AlarmAlertFullScreen.this, displayTime, Toast.LENGTH_LONG).show();
stopService(new Intent(Alarms.ALARM_ALERT_ACTION)); finish();
}
? 取消(dismiss)
private void dismiss(boolean killed) {
Log.i(killed ? \
// The service told us that the alarm has been killed, do not modify // the notification or stop the service. if (!killed) {Log.v(\
// Cancel the notification and stop playing the alarm NotificationManager nm = getNotificationManager(); nm.cancel(mAlarm.id);
stopService(new Intent(Alarms.ALARM_ALERT_ACTION)); } finish();
}
15