-1

I am doing some support work on an app that a previous placement student developed which is an app that takes in user input say 5. This then means that an alarm will sound every 5 minutes.

Her app has came back to me as the alarm has a mind of its own and has been inconsistent. Here is the code that she is using:

 alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
        //set alert
        alertDialogBuilder
                .setTitle("IP Check frequency: " + time.getText() + " minutes")
                .setMessage("Processing commenced at \n" + startTime.getText())
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (currentTime.after(alarmTime)) {
                            Toast.makeText(MainActivity.this, "Missed first alert", Toast.LENGTH_LONG).show();
                        }
                        intent1 = new Intent(MainActivity.this, MyBroadcastReceiver.class);

                        i = Integer.parseInt(time.getText().toString());
                        scTime2 = (i * 60 * 1000); //5 minutes before set time

                        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                        manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        manager.setRepeating(AlarmManager.RTC_WAKEUP, timeCommenced, scTime2, pendingIntent);
                        Toast.makeText(MainActivity.this, "Alert Set", Toast.LENGTH_SHORT).show();
                        stopped.setVisibility(View.VISIBLE);
                        commenced1.setVisibility(View.GONE);

                        //Change editText to TextView
                        time.setVisibility(View.GONE);
                        timeText.setVisibility(View.VISIBLE);
                        timeText.setText(time.getText().toString());
                        processingText.setText(R.string.processing_commenced);

                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog = alertDialogBuilder.create();
        alertDialog.show();

    }
}

I can see that she is using SetRepeating() and I have read that from api level 19 plus this may be the problem.

I have tried using SetExact() but I am getting a red line under the variables inside the method.

Would anyone be able to tell me how I can keep the variables but get the consistency?

Thanks!

TheAlmac2
  • 193
  • 2
  • 15

2 Answers2

0

setRepeating and setExact take a different number of variables so you cant just use the same thing without making code changes.

If you want something to trigger at that exact time then you need to use setExact and every time the alarm fires you need to calculate the next time it should file automatically and use setExact again with a new time for it to trigger.

If you use setRepeating the OS can/will deffer the trigger time to a time where it can trigger multiple alarms together to save battery.

tyczj
  • 71,600
  • 54
  • 194
  • 296
0

You are right. according to https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,%20long,%20long,%20android.app.PendingIntent)

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

what they recommend you to do is to use set() instead of setRepeating() which would trigger once. once the alarm is triggered, call the set() again inside the handler or pendingIntent.

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • Ive tried using set() there but I still get red lines. from the variables inside my method which can I take out? – TheAlmac2 May 08 '18 at 15:37
  • Would you be able to give me some code on how to call the set() again inside a handler or my pending intent? – TheAlmac2 May 08 '18 at 15:47