package com.example.patrice.request_repeat_03;

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

import java.util.Calendar;

public class MainActivity extends AppCompatActivity
{

    private PendingIntent pendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //startAlert();
        start02();

    }

    public void startAlert()
    {


        //Intent intent = new Intent(this, receiver.class);
        //PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        //AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
        //Toast.makeText(this, "Alarm set in " + i + " seconds",   Toast.LENGTH_LONG).show();

        int i = 20;
        int delai = i*1000;
        Intent intent = new Intent(this, receiver.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am =  (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),  i * 1000, pendingIntent);
        Toast.makeText(this, "Alarm set in " + i + " seconds",   Toast.LENGTH_LONG).show();

    }

    public void start02()
    {

        Intent myIntent = new Intent(this, receiver.class);

        //pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);

        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


        Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();

    }

}
