Как планировать задачу с помощью диспетчера аварийных сигналов

Я новичок в Android. Я работаю над приложением, в котором мне нужно запланировать задачу, которая будет выполнена в будущем.

Я прочитал о AlarmManager и узнаю, что мы можем выполнить это с помощью AlarmManager.

Может кто-нибудь рассказать мне о каком-то учебнике или о любом источнике, где я могу получить вещи.

Ответы

Ответ 1

Содержимое, взятое из блога Планирование задачи с помощью Alarm Manager в Android

public void scheduleAlarm(View v)
{
    // The time at which the alarm will be scheduled. Here the alarm is scheduled for 1 day from the current time. 
    // We fetch the current time in milliseconds and add 1 day time
    // i.e. 24*60*60*1000 = 86,400,000 milliseconds in a day.       
    Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;

    // Create an Intent and set the class that will execute when the Alarm triggers. Here we have
    // specified AlarmReceiver in the Intent. The onReceive() method of this class will execute when the broadcast from your alarm is received.
    Intent intentAlarm = new Intent(this, AlarmReceiver.class);

    // Get the Alarm Service.
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Set the alarm for a particular time.
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(this, "Alarm Scheduled for Tomorrow", Toast.LENGTH_LONG).show();       
}

Класс AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver
{
     @Override
     public void onReceive(Context context, Intent intent)
     {

         // Your code to execute when the alarm triggers
         // and the broadcast is received.   

     }
}

Ответ 2

Получить службу оповещения и установить будильник:

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("com.xxxxx.tq.TQServiceManager"), PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000 , 30 * 1000 , pendingIntent);

U нужен широковещательный приемник, который получит это событие

class Alarmreceiver extends Broadcastreceiver
{
   //u can to task in onreceive method of receiver.
}

Не забудьте зарегистрировать этот ресивер в файле Androidmanifest. Надеюсь, это поможет вам.