睡眠助手
闲来无聊 , 练手做一个android的小应用。功能:有一个开关控制该应用的开启和关闭; 能设置时间,时间到了可以闹铃的方式将主人闹醒; 记录开启之前的手机情景模式,在开启该应用时自动将情景模式切换成静音模式,并在时间到了的时候自动将情景模式切换成开启之前的模式; 在开启到时间到(或手动关闭)之间来电话,手机自动挂断,并会回复一条短信给来电号码,告知您拨打的用户正在睡觉,回复的内容可自己编辑也可调用模板,并将来电号码以notification的方式通知主人有未接来电。
第一天
第一天主要是完成一些布局文件和一些控件的基本实现。
一、首先是布局文件的设置
main.xml :<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="250px" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="100px" android:layout_height="wrap_content" android:text="时间显示" android:padding="5px" /> <TextView android:id="@+id/timeShow" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="100px" android:layout_height="wrap_content" android:text="回复内容显示" android:padding="5px" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/reply_show" android:padding="5px" android:hint="@string/content_hint" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="100px" android:layout_height="wrap_content" android:text="铃声显示" android:padding="5px" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ringtone_show" android:padding="5px" android:hint="卡农" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="100px" android:layout_height="wrap_content" android:text="模式显示" android:padding="5px" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mode_show" android:padding="5px" android:hint="睡眠" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="100px" android:layout_height="wrap_content" android:text="开关显示" android:padding="5px" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/on_off_show" android:padding="5px" android:hint="关" /> </LinearLayout> </LinearLayout> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableRow> <Button android:layout_column="1" android:id="@+id/setTime" android:text="设置时间" android:layout_width="150px" android:layout_height="wrap_content" /> <Button android:text="设置回复内容" android:id="@+id/setReply" android:layout_width="150px" android:layout_height="wrap_content" /> </TableRow> <TableRow> <Button android:layout_column="1" android:id="@+id/setRingTone" android:text="设置铃声" android:layout_width="150px" android:layout_height="wrap_content" /> <Button android:text="设置模式" android:id="@+id/setMode" android:layout_width="150px" android:layout_height="wrap_content" /> </TableRow> <TableRow> <Button android:layout_column="1" android:text="设置开关" android:id="@+id/setOnOff" android:layout_width="150px" android:layout_height="wrap_content" /> <Button android:text="查看帮助信息" android:id="@+id/help" android:layout_width="150px" android:layout_height="wrap_content" /> </TableRow> </TableLayout> </LinearLayout>
效果如下图:
设置了几个button来控制一些设置,并将一些设置的内容在textView中显示给用户。
1、设置时间是调用系统的一个TimePicker接口2、设置回复内容,是弹出一个自定义的可编辑的对话框来实现3、设置应用的开关,是弹出一个单选框的对话框。
代码如下:设置时间://设置时间按钮事件监听 class SetTimeListener implements OnClickListener { Time t = new Time(); @Override public void onClick(View v) { //设置成当前系统时间 t.setToNow(); new TimePickerDialog(HelperMain.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub String time ; if(hourOfDay > 12) { if((hourOfDay - 12) >= 10) { time = (hourOfDay - 12) + " : " + minute + " pm" ; } else { time = "0" + (hourOfDay - 12) + " : " + minute + " pm" ; } } else if(hourOfDay == 12) { time = hourOfDay + " : " + minute + " pm" ; }else { if(hourOfDay >= 10){ time = hourOfDay + " : " + minute + " am" ; } else { time = "0" + hourOfDay + " : " + minute + " am" ; } } timeShow.setText(time); SharedPreferences uiState = getPreferences(0); SharedPreferences.Editor editor = uiState.edit(); editor.putString("time", time); editor.commit(); } }, t.hour, t.minute, false).show(); } }
timer的xml文件的设置:
time.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>设置时间效果如下图:
![]()
设置回复内容://设置回复内容按钮事件监听 class SetReplyListener implements OnClickListener { @Override public void onClick(View v) { final EditText editReply = new EditText(HelperMain.this); new AlertDialog.Builder(HelperMain.this) .setTitle("设置回复内容") .setView(editReply) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub StringBuffer replyContent = new StringBuffer(); if(editReply.getText().length() >= 45) { //获取前45个字符 replyContent.append(editReply.getText(),0,44); } else { replyContent.append(editReply.getText()); } replyShow.setText(replyContent.toString()); SharedPreferences uiState = getPreferences(0); SharedPreferences.Editor editor = uiState.edit(); editor.putString("reply",replyContent.toString()); editor.commit();//一定要记得commit,不然会获取不到值 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create().show(); } }
回复内容的xml设置:
设置应用的开关:reply.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/editReply" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
设置回复内容效果如下图:
![]()
//设置时间按钮事件监听 class SetTimeListener implements OnClickListener { Time t = new Time(); @Override public void onClick(View v) { //设置成当前系统时间 t.setToNow(); new TimePickerDialog(HelperMain.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub String time ; if(hourOfDay > 12) { if((hourOfDay - 12) >= 10) { time = (hourOfDay - 12) + " : " + minute + " pm" ; } else { time = "0" + (hourOfDay - 12) + " : " + minute + " pm" ; } } else if(hourOfDay == 12) { time = hourOfDay + " : " + minute + " pm" ; }else { if(hourOfDay >= 10){ time = hourOfDay + " : " + minute + " am" ; } else { time = "0" + hourOfDay + " : " + minute + " am" ; } } timeShow.setText(time); SharedPreferences uiState = getPreferences(0); SharedPreferences.Editor editor = uiState.edit(); editor.putString("time", time); //一定要记得在存入数据之后要调用commit方法,不然getXXX的时候会获取不到值 editor.commit(); } }, t.hour, t.minute, false).show(); } }
设置开关效果如下图:![]()
本文介绍了一款名为“睡眠助手”的Android应用开发过程。该应用具备时间设置、回复内容编辑等功能,能在设定时间唤醒用户,并自动调整手机模式为静音。



352

被折叠的 条评论
为什么被折叠?



