package com.example.repeatingalarm;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author Prabu
 *
 */
public class MainActivity extends Activity {

	
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public void startAlert(View view)
	{
		
//		EditText text = (EditText) findViewById(R.id.time);
//		int i = Integer.parseInt(text.getText().toString());
//		Intent intent = new Intent(this, MyBroadcastReceiver.class);
//		PendingIntent pendingIntent = PendingIntent.getBroadcast(
//				this.getApplicationContext(), 234324243, intent, 0);
//		AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		/*alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
				+ (i * 1000), (i * 1000), pendingIntent);*/
//		alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
//				+ (i * 1000), 8000, pendingIntent);
//		Toast.makeText(this, "Starting alarm in " + i + " seconds",
//				Toast.LENGTH_LONG).show();
		
		
		
		EditText text = (EditText) findViewById(R.id.time);
		int i = Integer.parseInt(text.getText().toString());
		Intent intent = new Intent(this, MyBroadcastReceiver.class);
		pendingIntent = PendingIntent.getBroadcast(
				this.getApplicationContext(), 234324243, intent, 0);
		alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		/*alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
				+ (i * 1000), (i * 1000), pendingIntent);*/
		alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
				+ (i * 1000), 8000, pendingIntent);
		Toast.makeText(this, "Starting alarm in " + i + " seconds",
				Toast.LENGTH_LONG).show();
		
		
		
	}
	
	@Override

	protected void onDestroy() 
	{

		alarmManager.cancel(pendingIntent);

	       //unregisterReceiver(br);

	       super.onDestroy();

	}

}
