package android.csoft.HomeWork6_iwasax;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;


public class HomeWork6_iwasaxService extends Service{
	private PendingIntent alarmSender;
	
	public IBinder onBind(Intent intent) {
		return null;   
	}   
	//サービス起動時
	public void onStart(Intent intent, int startId) {
		//ノーティフィケーションの表示
		Thread thr = new Thread(null, task, "AlarmService_Service");
		thr.start();  
		showNotifibation(this,R.drawable.icon,"サービススタート","1分経過","サービスは継続します。");
	}

   private void showNotifibation(
			Context context, int icon,
			String ticker,String title,String message) {
		// ノーティフィケーションマネージャの取得
		NotificationManager nm;
		nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
		
		//ノーティフィケーションオブジェクト生成	
		Notification notification = 
				new Notification(icon, ticker, System.currentTimeMillis());
		PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(context,android.csoft.HomeWork6_iwasax.HomeWork6_iwasaxActivity.class), 0);
		notification.setLatestEventInfo(context, title,message,intent);

			// キャンセル
			nm.cancel(0);
			//表示
			nm.notify(0, notification);
	}
	   
	private Runnable task = new Runnable() {
		public void run() {  
			
			
		// 次回起動登録   
		long now = System.currentTimeMillis();   
		alarmSender = PendingIntent.getService(HomeWork6_iwasaxService.this, 0,    
		new Intent(HomeWork6_iwasaxService.this, HomeWork6_iwasaxService.class), 0);   
		AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);   
		am.set(AlarmManager.RTC, now + 600, alarmSender);   
		// サービス終了   
		HomeWork6_iwasaxService.this.stopSelf(); 
		} 
	};	


	

}  