package com.example.service02;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Environment;
import android.os.SystemClock;
import android.widget.Toast;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Service_code extends IntentService 
{
	
		private static final String TAG = "Service_code";
		
		
		public Service_code() 
		{
			super(TAG);
		}
	
		
		@Override
		protected void onHandleIntent(Intent intent) 
		{
			String content = "hello world";
			File file;
			FileOutputStream outputStream;
			try {
			    file = new File(Environment.getExternalStorageDirectory(), "BBBBBBBBBBBBBBBBBBBBBBBBB.txt");
			 
			    outputStream = new FileOutputStream(file);
			    outputStream.write(content.getBytes());
			    outputStream.close();
			} catch (IOException e) {
			    e.printStackTrace();
			}
			
			Intent i;
			PendingIntent pi;				
			AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
			i = new Intent(this, Receiver.class);
			
			pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
			
			
			Calendar calendar = Calendar.getInstance();
		     
		    calendar.set(Calendar.MONTH, 10);
		    calendar.set(Calendar.YEAR, 2016);
		    calendar.set(Calendar.DAY_OF_MONTH, 6);
		 
		    calendar.set(Calendar.HOUR_OF_DAY, 18);
		    calendar.set(Calendar.MINUTE, 54);
		    calendar.set(Calendar.SECOND, 0);
		    calendar.set(Calendar.AM_PM,Calendar.PM); // PM entre 12H et 23H
			
			am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
			
			/*
			String fileName = "pipotron.txt";
			File output = new File(Environment.getExternalStorageDirectory(),fileName);
			if(output.exists())
			{
			    output.delete();      
			}   
			
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(fcontent);
            bw.close();
*/
			
			/* 
			 InputStream stream = null;
			 FileOutputStream fos = null;
			 try{
			    URL url = new URL(urlPath);
			    stream = url.openConnection().getInputStream();
			    InputStreamReader reader = new InputStreamReader(stream);
			    fos = new FileOutputStream(output.getPath());
			    int next = -1;
			    while((next==reader.read()) != -1){
			  fos.write(next);   
			    }
			*/
		}
	
	
	
}
