一款Android后台服务定时提醒类应用

2019-08-22  本文已影响0人  _好好学习

项目地址:Todo V1.0,欢迎starissue

演示运行过程

运行演示

主要思路

具体剖析

添加todo
写一个Todo类用以在SQLite存取事项

// TODO: 2019/8/21 将数据写入
SQLiteSQLiteOpenHelper dbHelper = new DatabaseHelper(this, "TODO", null, 1);
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put("id", 1); //只有在第一次提交时需要执行或者用一个布尔变量判断是否是第一次上传数据
values.put("title", editText.getText().toString());values.put("date", dateText.getText().toString());
values.put("time", timeText.getText().toString());values.put("code", remindTypeCode);
sqLiteDatabase.insert("todo", null, values);Log.d(TAG, "addTodo: " + "写入数据库");
sqLiteDatabase.close();

展示todo
从SQLite读取todo数据并展示到listview


// TODO: 2019/8/21 从SQLite数据库中查询数据
DatabaseHelper databaseHelper = new DatabaseHelper(this, "TODO", null, 1);
SQLiteDatabase sqLiteDatabase = databaseHelper.getReadableDatabase();
// TODO: 2019/8/21 遍历查询所有todo对象
String sql = "select * from todo";Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
Todo todo;
while (cursor.moveToNext()) {
    todo = new Todo();
    todo.setTodo(cursor.getString(cursor.getColumnIndex("title")));
    todo.setDate(cursor.getString(cursor.getColumnIndex("date")));
    todo.setTime(cursor.getString(cursor.getColumnIndex("time")));
    todo.setCode(cursor.getInt(cursor.getColumnIndex("code")));
    Log.d(TAG, "load(): " + todo.getTodo());todoLists.add(todo);
}
sqLiteDatabase.close();

删除todo


DatabaseHelper databaseHelper = new DatabaseHelper(getContext(), "TODO", null, 1);
SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
//执行多条件sql语句进行删除具体数据项
String sql = "delete from todo where title = '" + todo.getTodo() +"' and date = '" + todo.getDate() + "' and time = '" + todo.getTime() + "' and code = '" + todo.getCode() + "'";
sqLiteDatabase.execSQL(sql);sqLiteDatabase.close();

接受广播与提醒
在AlarmReceiver的onReceive方法中接受广播并跳转至AlertActivity


Intent toAlertAct = new Intent(context, AlertActivity.class);
toAlertAct.putExtra("todo", intent.getStringExtra("todo"));
toAlertAct.putExtra("remindTypeCode", intent.getIntExtra("remindTypeCode", 0));
Log.d(TAG, "onReceive: " + intent.getStringExtra("todo") + intent.getIntExtra("remindTypeCode", 0));
toAlertAct.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(toAlertAct);

踩过的坑

本文为原创文章,未经允许不得转载

上一篇 下一篇

猜你喜欢

热点阅读