package com.chitraksh.tutorial.chilearn4;
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.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;public class NotificationActivity extends Activity implements View.OnClickListener{
private static final int NOTE_ID = 100;
private Handler handler = new Handler();
private Runnable task=new Runnable() {
@Override
public void run() {
NotificationManager mgr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent lanuchIntent=new Intent(getApplicationContext(),NotificationActivity.class);
PendingIntent contentIntent=PendingIntent.getActivity(getApplicationContext(),
0, lanuchIntent, 0);
//Create notification with the time it was fired
Notification note=new Notification(R.drawable.ic_plusone_tall_off_client,
"somtthing happens",System.currentTimeMillis());
//Set notification information
note.setLatestEventInfo(getApplicationContext(), "We are finished",
"Click here", contentIntent);
note.defaults|=Notification.DEFAULT_SOUND;
note.flags|=Notification.FLAG_AUTO_CANCEL;
mgr.notify(NOTE_ID,note);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn=new Button(this);
btn.setText("Post new Notification");
btn.setOnClickListener(this);
setContentView(btn);
}
@Override
public void onClick(View v) {
//Run 10 seconds after click
handler.postDelayed(task, 10000);
Toast.makeText(this, "Notification will post in 10secs",
Toast.LENGTH_SHORT).show();
}
}