Ich bin gerade neu in Android und ich arbeite derzeit auf Scheduler App.Dies ist mein Code auf die Einstellung Alarm mit SQLite dB Werte. Mein Problem hier ist, dass der Alarm auf zufällige Zeit auslöst.Android Alarm Manager - Alarm feuert auf zufällige Zeit
public void startAlarm() throws 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();
}
}
können Sie [siehe diese] (https://stackoverflow.com/a/35127736/5860777) –