思路大致是这样的:
1.用户手动输入(模拟服务器发来的消息),将用药提醒时间存入SQLITE数据库,同时,每隔2分钟的间隔录入一个时间点,持续录入15个(即30分钟),每隔10分钟的间隔录入一个时间点,持续录入9个(即90分钟),共计2小时,24个标记位状态都列为0;
2.如果到了时间点,响起闹铃,用户取消,所有的标志位都重新置为1;
3.如果到了SQLITE录入时间点3小时以后的时间,所有的标志位重新置为0;
4.监听进程一直获取当前的时间,如果当前的时间到了数据库记录的时间点,并且标志位是0,则会响起闹铃。
将来的扩展:
1.数据库要增加用户字段,因为一个报警设备有多个用户,并且每个用户最多可以设置三个时间点,入库过程之中是这样的存储结构。
id time user
0 13:43 zzk
1 13;45 tom
2 20:00 tom
3 21:00 tom
2.时间点存储如下所示,根据不同的用户,分别新建不一样的文件:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="10:01">1</string> <string name="10:03">1</string> <string name="10:23">1</string> <string name="09:53">1</string> <string name="09:51">1</string> <string name="10:13">1</string> <string name="09:41">1</string> <string name="11:13">1</string> <string name="11:23">1</string> <string name="11:33">1</string> <string name="10:53">1</string> <string name="11:03">1</string> <string name="10:33">1</string> <string name="10:43">1</string> <string name="09:39">1</string> <string name="09:37">1</string> <string name="09:35">1</string> <string name="09:33">1</string> <string name="09:43">1</string> <string name="09:57">1</string> <string name="09:45">1</string> <string name="09:55">1</string> <string name="09:47">1</string> <string name="09:49">1</string> <string name="09:59">1</string> </map>
3.以下贴代码:
DB工具包:
package com.zzk.util; /* import相关class */ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DailyBgDB extends SQLiteOpenHelper { /* 变量声明 */ private final static String DATABASE_NAME = "dailyBG_db"; private final static int DATABASE_VERSION = 1; private final static String TABLE_NAME = "dailySetting_table"; public final static String FIELD1 = "DailyId"; public final static String FIELD2 = "DailyTm"; public SQLiteDatabase sdb; /* 构造符 */ public DailyBgDB(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); sdb= this.getWritableDatabase(); } @Override public void onCreate(SQLiteDatabase db) { /* Table不存在就创建table */ String sql = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"("+FIELD1 +" INTEGER primary key, "+FIELD2+" TEXT)"; db.execSQL(sql); /* 初始的资料到DB */ sdb=db; // insert(0,99); // insert(1,99); // insert(2,99); // insert(3,99); // insert(4,99); // insert(5,99); // insert(6,99); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public Cursor select() { Cursor cursor=sdb.query(TABLE_NAME,null,null,null,null,null,null); return cursor; } /* select时有where条件要用否method */ public Cursor select(String selection,String[] selectionArgs) { String[] columns = new String[] { FIELD2 }; Cursor cursor=sdb.query(TABLE_NAME,columns,selection,selectionArgs, null,null,null); return cursor; } public long insert(int value1,String value2) { /* 将新增的值放入ContentValues */ ContentValues cv = new ContentValues(); cv.put(FIELD1, value1); cv.put(FIELD2, value2); long row = sdb.insert(TABLE_NAME, null, cv); return row; } public void delete(int id) { String where = FIELD1 + " = ?"; String[] whereValue ={ Integer.toString(id) }; sdb.delete(TABLE_NAME, where, whereValue); } public void update(int id, int value) { String where = FIELD1 + " = ?"; String[] whereValue ={ Integer.toString(id) }; /* 将修改的值放入ContentValues */ ContentValues cv = new ContentValues(); cv.put(FIELD2, value); sdb.update(TABLE_NAME, cv, where, whereValue); } }
页面:
package net.blogjava.mobile; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import com.zzk.util.DailyBgDB; import android.app.Activity; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class Main extends Activity implements OnClickListener { private TextView tvAlarmRecord; private SharedPreferences sharedPreferences; private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); private DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); private AlarmManager alarmManager; private PendingIntent pendingIntent; public String setTime; /* 声明定义的数据库变量DailyBgDB */ private DailyBgDB db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnAddAlarm = (Button) findViewById(R.id.btnAddAlarm); tvAlarmRecord = (TextView) findViewById(R.id.tvAlarmRecord); btnAddAlarm.setOnClickListener(this); sharedPreferences = getSharedPreferences("alarm_record", Activity.MODE_PRIVATE); alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class); // 创建封装BroadcastReceiver 的pendingIntent对象 pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); // 开始定时器,每1分钟执行一次,5s alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 10000, pendingIntent); // alarmManager.cancel(pendingIntent); Button cancel = (Button) findViewById(R.id.button1); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { Log.e("setTime", "" + setTime); updateAlarm(setTime,1); } }); } @Override public void onClick(View v) { View view = getLayoutInflater().inflate(R.layout.alarm, null); final TimePicker timePicker = (TimePicker) view .findViewById(R.id.timepicker); timePicker.setIs24HourView(true); // 显示设置时间点的对话框 new AlertDialog.Builder(this).setTitle("设置提醒时间").setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String timeStr = String.valueOf(timePicker .getCurrentHour()) + ":" + String.valueOf(timePicker.getCurrentMinute()); // 将时间点添加到 TextView 组件中 tvAlarmRecord.setText(tvAlarmRecord.getText() .toString() + "\n" + timeStr); // count++; /* * View view2 = new View(Main.this); * view2.setLayoutParams(new * LayoutParams(LayoutParams.FILL_PARENT,1)); * view2.setBackgroundColor(Color.RED); LinearLayout * linearLayout = * (LinearLayout)getLayoutInflater().inflate * (R.layout.main, null); linearLayout.addView(view2); */ Log.e("hour", "" + timePicker.getCurrentHour()); Log.e("minute", "" + timePicker.getCurrentMinute()); Log.e("time point:", "--------" + timeStr); setTime = timeStr; setAlarm(timeStr); //将定时信息入数据库 //saveData(1,setTime); saveData(1,setTime); } }).setNegativeButton("取消", null).show(); } //timeStr是时间(小时+分钟) public void setAlarm(String timeStr) { Date date = null; String str = null; Calendar cal = Calendar.getInstance(); Date dateNow = cal.getTime(); str = format2.format(dateNow) + " " + timeStr; try { date = format1.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t = date.getTime(); System.out.println(t); for (int i = 0; i <= 14; i++) { Date now3 = new Date(t + 1000 * 60 * 2 * i); // 每两分钟输出一次 String time3 = formatter.format(now3); System.out.println("十分钟后的时间是:" + time3); // 保存时间点 sharedPreferences.edit().putString(time3, 0+"") .commit(); } System.out.println("---------------30分钟以后的时间是---------"); for (int i = 0; i <= 9; i++) { Date now4 = new Date(t + 1000 * 60 * 2 * 15 + 1000 * 60 * 10 * i); String time4 = formatter.format(now4); System.out.println("十分钟后的时间是:" + time4); // 保存时间点 sharedPreferences.edit().putString(time4, 0+"") .commit(); } } //timeStr是时间(小时+分钟) public void updateAlarm(String timeStr,int type) { Date date = null; String str = null; Calendar cal = Calendar.getInstance(); Date dateNow = cal.getTime(); str = format2.format(dateNow) + " " + timeStr; try { date = format1.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t = date.getTime(); System.out.println(t); for (int i = 0; i <= 14; i++) { Date now3 = new Date(t + 1000 * 60 * 2 * i); // 每两分钟输出一次 String time3 = formatter.format(now3); System.out.println("十分钟后的时间是:" + time3); // 保存时间点 sharedPreferences.edit().putString(time3, type+"") .commit(); } System.out .println("---------------30分钟以后的时间是---------"); for (int i = 0; i <= 9; i++) { Date now4 = new Date(t + 1000 * 60 * 2 * 15 + 1000 * 60 * 10 * i); String time4 = formatter.format(now4); System.out.println("十分钟后的时间是:" + time4); // 保存时间点 sharedPreferences.edit().putString(time4, type+"") .commit(); } } /* 储存设定值吹DB的method */ private void saveData(int id,String value){ db=new DailyBgDB(Main.this); db.insert(id,value); db.close(); } }
监听程序:
package net.blogjava.mobile; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import com.zzk.util.DailyBgDB; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.media.MediaPlayer; import android.util.Log; public class AlarmReceiver extends BroadcastReceiver { private static int num = 0; /* 声明定义的数据库变量DailyBgDB */ private DailyBgDB db; private SharedPreferences sharedPreferences; private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); private DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); @Override public void onReceive(Context context, Intent intent) { sharedPreferences = context.getSharedPreferences( "alarm_record", Activity.MODE_PRIVATE); String hour = String.valueOf(Calendar.getInstance().get( Calendar.HOUR_OF_DAY)); String minute = String.valueOf(Calendar.getInstance().get( Calendar.MINUTE)); if(minute.length()==1){ minute="0"+minute; } /*** * 监控程序 * 第一步、获取数据库中的时间 * 第二步,将数据库中的时间换算成两小时以后的时间 * 第三步,如果当前时间到了两小时以后的时间,更新XML */ /* 储存设定值吹DB的method */ db=new DailyBgDB(context); //获取数据库中的时间 Cursor cur=db.select("DailyId=1", null); while(cur.moveToNext()) { cur.getInt(0); //Log.e("----1-------", cur.getString(cur.getColumnIndex("DailyId"))); Log.e("----2-------", cur.getString(0)); if(get2HourLaterTime(cur.getString(0)).equalsIgnoreCase(hour + ":" + minute)) { updateAlarm(cur.getString(0), 0); } cur.close(); } Log.e("ActionBroadCast", "New Message !" + num++); // 从XML 文件夹获得描述当前时间点的value String time = sharedPreferences.getString(hour + ":" + minute, null); if (time != null) { if(time.equalsIgnoreCase("0")) { Log.e("value", time); // 播放声音 MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.ring); mediaPlayer.start(); } } db.close(); } //timeStr是时间(小时+分钟) public void updateAlarm(String timeStr,int type) { Date date = null; String str = null; Calendar cal = Calendar.getInstance(); Date dateNow = cal.getTime(); str = format2.format(dateNow) + " " + timeStr; try { date = format1.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t = date.getTime(); System.out.println(t); for (int i = 0; i <= 14; i++) { Date now3 = new Date(t + 1000 * 60 * 2 * i); // 每两分钟输出一次 String time3 = formatter.format(now3); System.out.println("十分钟后的时间是:" + time3); // 保存时间点 sharedPreferences.edit().putString(time3, type+"") .commit(); } System.out .println("---------------30分钟以后的时间是---------"); for (int i = 0; i <= 9; i++) { Date now4 = new Date(t + 1000 * 60 * 2 * 15 + 1000 * 60 * 10 * i); String time4 = formatter.format(now4); System.out.println("十分钟后的时间是:" + time4); // 保存时间点 sharedPreferences.edit().putString(time4, type+"") .commit(); } } public String get2HourLaterTime(String timeStr) { Date date = null; String str = null; Calendar cal = Calendar.getInstance(); Date dateNow = cal.getTime(); str = format2.format(dateNow) + " " + timeStr; try { date = format1.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t = date.getTime(); System.out.println(t); Date now3 = new Date(t + 1000 * 60 * 60 * 3); //Date now3 = new Date(t + 1000 * 60 * 2); return formatter.format(now3); } }
源代码地址是:
http://download.csdn.net/detail/opzoonzhuzhengke/5817857
时间: 2024-11-03 04:23:57