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!
<receiver android:name='.AlarmManagerEx.AlarmReceiver'> </receiver>(Inside the<application>) - Pasha Oleynik