要使用Android AlarmManager与SQL进行操作,你可以按照以下步骤进行:
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "alarms_db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_ALARMS = "alarms";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_TIME = "time";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ALARMS_TABLE = "CREATE TABLE " + TABLE_ALARMS +
"(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_TITLE + " TEXT," +
COLUMN_TIME + " INTEGER" +
")";
db.execSQL(CREATE_ALARMS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALARMS);
onCreate(db);
}
}
public class Alarm {
private int id;
private String title;
private long time;
public Alarm() {
}
public Alarm(int id, String title, long time) {
this.id = id;
this.title = title;
this.time = time;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
public class AlarmDataSource {
private SQLiteDatabase database;
private DbHelper dbHelper;
public AlarmDataSource(Context context) {
dbHelper = new DbHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void createAlarm(String title, long time) {
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_TITLE, title);
values.put(DbHelper.COLUMN_TIME, time);
database.insert(DbHelper.TABLE_ALARMS, null, values);
}
public List getAllAlarms() {
List alarms = new ArrayList<>();
Cursor cursor = database.query(DbHelper.TABLE_ALARMS, null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
int id = cursor.getInt(cursor.getColumnIndex(DbHelper.COLUMN_ID));
String title = cursor.getString(cursor.getColumnIndex(DbHelper.COLUMN_TITLE));
long time = cursor.getLong(cursor.getColumnIndex(DbHelper.COLUMN_TIME));
Alarm alarm = new Alarm(id, title, time);
alarms.add(alarm);
cursor.moveToNext();
}
cursor.close();
return alarms;
}
public void deleteAlarm(int id) {
database.delete(DbHelper.TABLE_ALARMS, DbHelper.COLUMN_ID + " = " + id, null);
}
}
public class MainActivity extends AppCompatActivity {
private AlarmDataSource dataSource;
private AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource = new AlarmDataSource(this);
dataSource.open();
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// 创建闹钟
dataSource.createAlarm("闹钟1", System.currentTimeMillis() + 10000);
// 获取所有闹钟
List alarms = dataSource.getAllAlarms();
for (Alarm alarm : alarms) {
// 设置闹钟
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("alarm_title", alarm.getTitle());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, alarm.getId(), intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getTime(), pendingIntent);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
dataSource.close();
}
}