2017-09-21 2 views
1

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(); 

       } 
} 
+0

können Sie [siehe diese] (https://stackoverflow.com/a/35127736/5860777) –

Antwort

0

Nach Android 4.4 hat Google OS Power-Management-Mechanismus eingeführt Stromverbrauch zu minimieren. Versuchen Sie, eine Version hinzuzufügen, die Ihren Code ähnlich wie den folgenden überprüft. Wenn die Version KitKat und höher ist, verwenden Sie setExact(). Ansonsten verwende set().

Siehe Beispiel unten:

 //For Android version 4.4 up, use setExact() for the alarm 
     //otherwise use set() 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 
      alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncInterval * 1000, pendingIntent); 
     } else { 
      alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncInterval * 1000, pendingIntent); 
     } 

Documentation reference here

+0

Okay, danke ich werde versuchen, –

+0

gleich, nachdem die App ausgeführt wird, wird der Alarm nach 3 Sekunden auslöst. –

+0

Ist es nach 3 Sekunden konsistent? – ADimaano

0

Versuchen Sie diesen Satz von Code Sie da nie aufhören würde es für mich funktioniert. "setInexactReapeating" funktioniert nicht in vielen der Lollipop- und Marshmallows-Geräte, wenn es einmal gestartet ist, dann verlieren Sie die Kontrolle über den Service, dies wird Ihnen helfen.

if (android.os.Build.VERSION.SDK_INT >= 23) 
{ 
    piLR = PendingIntent.getBroadcast(context, 0, intentLR, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
    amLR.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       interval, piLR); 
} 
else if (android.os.Build.VERSION.SDK_INT >= 19 
      && android.os.Build.VERSION.SDK_INT < 23) 
{ 
    piLR = PendingIntent.getBroadcast(context, 0, intentLR, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
    amLR.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis(), interval, piLR); 
} 
else 
{ 
    amLR.setRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis(), interval, piLR); 
}