第二阶段冲刺05
今天尝试做了做闹钟提醒,学了学相关的内容。
public class AlarmView extends LinearLayout { @Override protected void onFinishInflate() { super.onFinishInflate(); btnAddAlarm = (Button)findViewById(R.id.btnAddAlarm); lvAlarmList = (ListView)findViewById(R.id.lvAlarmList); lvAlarmList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { new AlertDialog.Builder(getContext()).setTitle("操作选项").setItems(new CharSequence[]{"删除","添加"}, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: deleteAlarm(position); break; case 1: addAlarm(); default: break; } } }).setNegativeButton("取消",null).show(); return true; } }); adapter = new ArrayAdapter<AlarmData>(getContext(),android.R.layout.simple_list_item_1); lvAlarmList.setAdapter(adapter); readSaveAlarmList(); //adapter.add(new AlarmData(System.currentTimeMillis())); alarmManager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE); calendar = Calendar.getInstance(); btnAddAlarm = (Button)findViewById(R.id.btnAddAlarm); btnAddAlarm.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ addAlarm(); } }); } private void deleteAlarm(int position){ AlarmData ad = adapter.getItem(position); adapter.remove(ad); adapter.notifyDataSetChanged(); saveAlarmList(); alarmManager.cancel(PendingIntent.getBroadcast(getContext(),ad.getId(),new Intent(getContext(),AlarmReceiver.class),0)); } private void addAlarm(){ Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); new TimePickerDialog(getContext(),new TimePickerDialog.OnTimeSetListener(){ @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,hourOfDay); calendar.set(Calendar.MINUTE,minute); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Calendar currentTime = Calendar.getInstance(); if(calendar.getTimeInMillis()<=currentTime.getTimeInMillis()){ calendar.setTimeInMillis(calendar.getTimeInMillis()+24*60*60*1000); } AlarmData ad = new AlarmData(calendar.getTimeInMillis()); adapter.add(ad); adapter.notifyDataSetChanged(); alarmManager.set(AlarmManager.RTC_WAKEUP, ad.getTime(),PendingIntent.getBroadcast(getContext(), ad.getId(),new Intent(getContext(),AlarmReceiver.class),0)); saveAlarmList(); } },c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),true).show(); } private void saveAlarmList(){ Editor editor; editor = getContext().getSharedPreferences(AlarmView.class.getName(), Context.MODE_PRIVATE).edit(); StringBuffer sb = new StringBuffer(); for (int i =0; i<adapter.getCount();i++){ sb.append(adapter.getItem(i).getTime()).append(","); } if(sb.length()>1){ String content = sb.toString().substring(0,sb.length()-1); editor.putString(KEY_ALARM_LIST,content); System.out.println(content); }else { editor.putString(KEY_ALARM_LIST,null); } editor.commit(); } private void readSaveAlarmList(){ SharedPreferences sp = getContext().getSharedPreferences( AlarmView.class.getName(),Context.MODE_PRIVATE); String content = sp.getString(KEY_ALARM_LIST,null); if(content != null){ String[] timeStrings = content.split(","); for(String string : timeStrings){ adapter.add(new AlarmData(Long.parseLong(string))); } } } private Button btnAddAlarm; private ListView lvAlarmList; private static final String KEY_ALARM_LIST = "alarmList"; private ArrayAdapter<AlarmData> adapter; private AlarmManager alarmManager; private Calendar calendar; private static class AlarmData{ private String timeLable = ""; private long time = 0; private Calendar date; public AlarmData(long time){ this.time = time; date = Calendar.getInstance(); date.setTimeInMillis(time); timeLable = String.format("%02d月%02d日 %02d:%02d", date.get(Calendar.MONTH)+1, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE)); } public AlarmData(String ad){ this.timeLable = ad; } public void setTime(long time){ this.time = time; } public long getTime(){ return time; } public void setTimeLable(String timeLable){ this.timeLable = timeLable; } public String getTimeLable(){ return timeLable; } @Override public String toString() { return getTimeLable(); } public int getId(){ return (int)(getTime()/1000/60); } } }