package com.samples.notificationmanager;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NotificationActivity extends Activity 
	implements View.OnClickListener { 

	private static final int ID_NOTIFY = 101;
	
	private NotificationManager manager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);	
		
		Button bGen = (Button)findViewById(R.id.bCreate);
		Button bClear = (Button)findViewById(R.id.bClear);
		
		bGen.setOnClickListener(this);
		bClear.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bCreate:
			manager = (NotificationManager)getSystemService(
					Context.NOTIFICATION_SERVICE);
			
			Notification notification = new Notification();
			notification.icon = android.R.drawable.ic_notification_overlay;
			notification.tickerText = "New Notification";
			notification.when = System.currentTimeMillis();
	        
			Intent intent = new Intent(this, NotificationActivity.class);
			
			PendingIntent pendIntent = PendingIntent.getActivity(
			    	getApplicationContext(), 0, intent, 0);		
			notification.setLatestEventInfo(this, "Notification!",
					"Notification from app", pendIntent);	
			
			manager.notify(ID_NOTIFY, notification);
			break;
			
		case R.id.bClear:
			manager.cancel(ID_NOTIFY);
			break;
		}		
	}
}