自己写一个安卓手机软件计算煮饭时间

来源:互联网 发布:淘宝卖家店名怎么改 编辑:程序博客网 时间:2024/05/16 02:12

最近这些日子,经常需要每天晚上在电饭锅上设置定时煮饭,以备第二天的早餐。但是计算倒计时时间是一件讨厌并且费脑子的事情。于是我趁今天周末写了这么一个Android手机软件,还开放源代码了。

“煮饭时间”——一个很简单的计算煮饭时间的Android手机软件,作者Liigo。

项目首页:https://github.com/liigo/cooktime

下载地址:CookTime.apk (270KB)

“煮饭时间”软件的主要功能是:

  • 计算并显示当前时间与开饭时间的时间间隔

  • 软件启动后、修改开饭时间后自动计算

  • 显示部分放在屏幕上方、操作部分放在屏幕下方,方便使用

  • 自动记忆修改后的开饭时间

  • 可以重新计算时间间隔

软件主界面如下:

  


软件核心Java源代码如下

package com.liigo.cooktime;public class MainActivity extends Activity {private TextView   textViewShowTime;private TimePicker timePickerMealTime;private Button     buttonCalcTime;SharedPreferences  pref;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textViewShowTime   = (TextView)   findViewById(R.id.textViewShowTime);timePickerMealTime = (TimePicker) findViewById(R.id.timePickerMealTime);buttonCalcTime     = (Button)     findViewById(R.id.buttonCalcTime);// 初始化开饭时间,恢复上次运行时存储的开饭时间pref = PreferenceManager.getDefaultSharedPreferences(this);timePickerMealTime.setIs24HourView(true);timePickerMealTime.setCurrentHour(pref.getInt("MealTime.Hour", 8));timePickerMealTime.setCurrentMinute(pref.getInt("MealTime.Minute", 0));calcTime(); // 计算并显示距离开饭时间的间隔时长buttonCalcTime.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {calcTime();}});timePickerMealTime.setOnTimeChangedListener(new OnTimeChangedListener() {@Overridepublic void onTimeChanged(TimePicker timePicker, int hour, int minute) {calcTime();// 存储开饭时间Editor editor = pref.edit();editor.putInt("MealTime.Hour", hour);editor.putInt("MealTime.Minute", minute);editor.commit();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}/** * 计算并显示当前时间与开饭时间之间间隔的时间差 */private void calcTime() {// 现在时间(本地时区)Calendar calendar = Calendar.getInstance();int fromHour   = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制int fromMinute = calendar.get(Calendar.MINUTE);// 开饭时间int endHour   = timePickerMealTime.getCurrentHour(); // 24小时制int endMinute = timePickerMealTime.getCurrentMinute();Log.d("cooktime", "from "+fromHour+":"+fromMinute+" to "+endHour+":"+endMinute);// 取二者时间间隔int hour, minute;if(fromHour > endHour || (fromHour == endHour && fromMinute > endMinute)) {endHour += 24;}hour = endHour - fromHour;minute = endMinute - fromMinute;if(minute < 0) {minute += 60;hour--;if(hour < 0) hour += 24;}String showtimeStr = "";showtimeStr += hour + "小时" + minute + "分钟";textViewShowTime.setText(showtimeStr);}}


1 0
原创粉丝点击