There is a table in the database in which each entry contains the day of the week, hours and minutes for the signal (something like an alarm clock). And, when the time comes, the activation should be triggered (at what, even in sleep mode, but these are details already). Here, I threw such code:

public class TaskScanner { Context context; DBHelper dbHelper; public TaskScanner (Context context, DBHelper dbHelper) { this.context = context; this.dbHelper = dbHelper; } /** * Сканирование задач из таблицы SCHEDULE * и установка их сигналов */ public void scanTasks () { int rows = dbHelper.getRowsQuantity("SCHEDULE"); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = db.query("SCHEDULE", null, null, null, null, null, null); int dayColIndex = c.getColumnIndex("DAY"); int hourColIndex = c.getColumnIndex("HOURS"); int minuteColIndex = c.getColumnIndex("MINUTES"); // Цикл для чтения таблицы БД и записи данных в переменные for (int i = 0; i < rows; i ++) { // Дефолтные значения переменных int hour = 0; int minute = 0; int dayOfWeek = 0; // Получение значений из БД if (c.moveToNext()) { hour = c.getInt(hourColIndex); minute = c.getInt(minuteColIndex); dayOfWeek = c.getInt(dayColIndex); } // Получение объекта календаря с текущим временем Calendar cal = Calendar.getInstance(); // добавление сигнала cal.set(Calendar.DAY_OF_WEEK, dayOfWeek + 1); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, 0); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); intent.putExtra("alarm message", "alarm message"); PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender); } } } 

And BroadcastReceiver :

 public class AlarmReceiver extends BroadcastReceiver { private static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { if (ACTION_BOOT.equals(intent.getAction())) { try { Bundle bundle = intent.getExtras(); String message = bundle.getString("alarm_message"); Intent newIntent = new Intent(context, SignalsActivity.class); newIntent.putExtra("alarm_message", message); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(newIntent); } catch (Exception e) { Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } } } 

Call the scanTasks () method in the MainActivity of onCreate ();

Does not work. (Which is probably not strange). Many thanks for your answers!

  • Most likely, you did not carefully read any example from the Internet and forgot to add the receiver's ad to the manifest. - JuriySPb
  • Is that <receiver android:name='.AlarmManagerEx.AlarmReceiver'> </receiver> (Inside the <application> ) - Pasha Oleynik
  • Like it, yes ... - Yuriy SPb
  • And by the way, you in your receiver only respond to the event of the final loading of the device. Do you really want this? ... - YurySPb
  • No, it is unfinished simply. But the problem is that after restarting the device, the code still does not work. Activation is not called. And I think this is due to the fact that the method call for adding signals occurs in the Activity. With this moment I did not understand. It may be necessary to call this method somewhere in the receiver, because it is he who is registered in the manifest, and as I understand it, after restarting, only the receiver works (from the entire application). Right? - Pasha Oleynik

0