package com.example.patrice.boot_03;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class StartAlarm extends BroadcastReceiver
{

    private static final int PERIOD = 60000;


    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            scheduleAlarms(context);
        }
    }


    static void scheduleAlarms(Context ctxt)
    {

        AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);

        Intent i = new Intent(ctxt, BackService.class);

        PendingIntent pi = PendingIntent.getService(ctxt, 0, i, 0);

       // mgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);

        long startTime = System.currentTimeMillis();
        long intervalTime = 60*1000;

        mgr.setRepeating(AlarmManager.RTC_WAKEUP, startTime, intervalTime, pi);

        Toast.makeText(ctxt, "Alarm repeating in " + i + " seconds", Toast.LENGTH_LONG).show();

    }


}