Im 그냥 새로운 안드로이드와 인스턴트 메신저에서 현재 일하고 있습니다. 이것은 SQLite db 값을 사용하여 알람을 설정하는 코드입니다. 여기 내 문제는 알람이 임의의 시간에 트리거된다는 것입니다.안드로이드 알람 관리자 - 임의의 시간에 알람 발화
공공 무효 startAlarm는()는 ParseException를 슬로우 {
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
String time = sdf.format(dt);
String start_time;
Calendar sCalendar = Calendar.getInstance();
String dayLongName = sCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase sqLiteDatabase = helper.getReadableDatabase();
String checktime = "Select * from SCHEDULE where week_day='"+dayLongName+"'";
Cursor cursor = sqLiteDatabase.rawQuery(checktime, null);
if(cursor.moveToNext()) {
start_time = cursor.getString(cursor.getColumnIndex(DatabaseHelper.STARTTIME));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a",Locale.getDefault());
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("hh:mm",Locale.getDefault());
Date milli =simpleDateFormat2.parse(start_time);
String milli2 = simpleDateFormat2.format(milli);
Date timeparsed1 = simpleDateFormat.parse(start_time);
String timeparsed2 = simpleDateFormat.format(timeparsed1);
String s = milli2;
Pattern p = Pattern.compile("(\\d+):(\\d+)");
Matcher m = p.matcher(s);
if (m.matches()) {
int hrs = Integer.parseInt(m.group(1));
int min = Integer.parseInt(m.group(2));
long ms = (long) hrs * 60 * 60 * 1000 + min * 60 * 1000;
// System.out.println("hrs=" + hrs + " min=" + min + " ms=" + ms);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, ms , pendingIntent);
//Toast.makeText(getApplicationContext(),String.valueOf(ms),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"Bad Format",Toast.LENGTH_SHORT).show();
}
}
당신은 [이 참조] 수 (https://stackoverflow.com/a/35127736/5860777) 나는 –