1

Im a just new in android and im currently working on scheduler app.This is my code on setting alarm using SQLite db values. My problem here is that the alarm is triggering on random time.

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

              }
}
CJ Caimoy
  • 51
  • 6

2 Answers2

0

After Android 4.4, Google has introduced OS power management mechanism to minimize power use. Try adding a version checking in your code similar to the one below. If the version is KitKat and above, use setExact(). Otherwise use set().

See example below:

        //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

ADimaano
  • 396
  • 1
  • 4
0

Try this set of code your service never going to stop it works for me. "setInexactReapeating" not working in many of the lollipop and marshmallows devices once it started then lose control over the service this will help you out.

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);
}
Andy Developer
  • 3,071
  • 1
  • 19
  • 39