AlarmManager 不及时出现
AlarmManager doesn't come up at time
我正在尝试构建闹钟,唤醒时间已在 SQLite 数据库中设置
我已经从数据库中得到了毫秒级的时间
但当时并没有报警
我认为这可能是一个糟糕的方法,如果有更好的方法请告诉我
下面是我的数据库class
public Cursor filList(long id) throws SQLException{
Cursor cursor = db.query(
"ALARM",
new String[] {"_id","Atime"},
"_id =" +id,
null,
null,
null,
null,
null
);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public int update(long rowId, String value) {
ContentValues args = new ContentValues();
args.put("Atime", value);
return db.update("ALARM", //資料表名稱
args, //VALUE
"_id=" + rowId, //WHERE
null //WHERE的參數
);
}
public boolean isEmpty(){
db.execSQL("CREATE TABLE IF NOT EXISTS"+ " ALARM " +"(_id INTEGER PRIMARY KEY , " + "Aname VAR UNIQUE , " +"Atime TIME)");
Cursor cursor = db.query(
"ALARM",
null,
null,
null,
null,
null,
null,
null
);
if (cursor.getCount() ==0) {
return false; //將指標移到第一筆資料
}
else
return true;
}
下面是从数据库中获取数据
if(helper.isEmpty()){
Cursor morn = helper.filList(1);
Cursor noo = helper.filList(2);
Cursor ni = helper.filList(3);
Cursor mid = helper.filList(4);
alarm(morn.getString(1));
alarm(noo.getString(1));
alarm(ni.getString(1));
alarm(mid.getString(1));
}
下面是报警
public void alarm (String alarmtimein) {
SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH:mm");
SimpleDateFormat date = new SimpleDateFormat("yyyy MM dd");
try {
Intent intent11 = new Intent(getApplicationContext(), PlayReceiver.class);
Date today = new Date();
String s = date.format(today);
String times = s + " " + alarmtimein;
Date alarmtimeout = time.parse(times);
long milliseconds = alarmtimeout.getTime();
intent11.putExtra("msg", "play_voice");
long elapsed = milliseconds;
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 1, intent11,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,elapsed , pi);
} catch (ParseException e) {
e.printStackTrace();
}
}
尝试用 setExact 替换 set 方法。
public void alarm (String alarmtimein) {
SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH:mm");
SimpleDateFormat date = new SimpleDateFormat("yyyy MM dd");
try {
Intent intent11 = new Intent(getApplicationContext(),
PlayReceiver.class);
Date today = new Date();
String s = date.format(today);
String times = s + " " + alarmtimein;
Date alarmtimeout = time.parse(times);
long milliseconds = alarmtimeout.getTime();
intent11.putExtra("msg", "play_voice");
long elapsed = milliseconds;
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
1, intent11,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP,elapsed , pi);
} catch (ParseException e) {
e.printStackTrace();
}}
我正在尝试构建闹钟,唤醒时间已在 SQLite 数据库中设置
我已经从数据库中得到了毫秒级的时间
但当时并没有报警
我认为这可能是一个糟糕的方法,如果有更好的方法请告诉我
下面是我的数据库class
public Cursor filList(long id) throws SQLException{
Cursor cursor = db.query(
"ALARM",
new String[] {"_id","Atime"},
"_id =" +id,
null,
null,
null,
null,
null
);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
public int update(long rowId, String value) {
ContentValues args = new ContentValues();
args.put("Atime", value);
return db.update("ALARM", //資料表名稱
args, //VALUE
"_id=" + rowId, //WHERE
null //WHERE的參數
);
}
public boolean isEmpty(){
db.execSQL("CREATE TABLE IF NOT EXISTS"+ " ALARM " +"(_id INTEGER PRIMARY KEY , " + "Aname VAR UNIQUE , " +"Atime TIME)");
Cursor cursor = db.query(
"ALARM",
null,
null,
null,
null,
null,
null,
null
);
if (cursor.getCount() ==0) {
return false; //將指標移到第一筆資料
}
else
return true;
}
下面是从数据库中获取数据
if(helper.isEmpty()){
Cursor morn = helper.filList(1);
Cursor noo = helper.filList(2);
Cursor ni = helper.filList(3);
Cursor mid = helper.filList(4);
alarm(morn.getString(1));
alarm(noo.getString(1));
alarm(ni.getString(1));
alarm(mid.getString(1));
}
下面是报警
public void alarm (String alarmtimein) {
SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH:mm");
SimpleDateFormat date = new SimpleDateFormat("yyyy MM dd");
try {
Intent intent11 = new Intent(getApplicationContext(), PlayReceiver.class);
Date today = new Date();
String s = date.format(today);
String times = s + " " + alarmtimein;
Date alarmtimeout = time.parse(times);
long milliseconds = alarmtimeout.getTime();
intent11.putExtra("msg", "play_voice");
long elapsed = milliseconds;
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 1, intent11,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,elapsed , pi);
} catch (ParseException e) {
e.printStackTrace();
}
}
尝试用 setExact 替换 set 方法。
public void alarm (String alarmtimein) {
SimpleDateFormat time = new SimpleDateFormat("yyyy MM dd HH:mm");
SimpleDateFormat date = new SimpleDateFormat("yyyy MM dd");
try {
Intent intent11 = new Intent(getApplicationContext(),
PlayReceiver.class);
Date today = new Date();
String s = date.format(today);
String times = s + " " + alarmtimein;
Date alarmtimeout = time.parse(times);
long milliseconds = alarmtimeout.getTime();
intent11.putExtra("msg", "play_voice");
long elapsed = milliseconds;
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
1, intent11,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setExact(AlarmManager.RTC_WAKEUP,elapsed , pi);
} catch (ParseException e) {
e.printStackTrace();
}}