Thursday 19 April 2012

Notification In Android

Your Application is Running in the Background  with no visibility , How u notify to the user an important event has occurred

the most convenient way is Use the STATUS BAR NOTIFICATION

here are  the steps to create the STATUS BAR NOTIFICATION

assume that you have a button in your activity so ,when ever you click on the button after some time you need to push the notification to the status bar

in general
1)get the button


Button notificationBtn;
NotificationManager notificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get thebuttton
           notificationBtn = (Button)findViewById(R.id.notificationBtn);
notificationBtn.setOnClickListener(this);    
}

whenever you  click on the button push the notification to the status bar after 10 minutes for this purpose i used handler Mechanism

create the Handler object

Handler handler = new Handler();

handling the onclick event of the button and pushing the notification by  calling the postDelay() to push the notification after 10 sec



@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==notificationBtn){
handler.postDelayed(runnable, 10000);
Toast.makeText(getApplicationContext(), "NOtification Will post in 10 sec", Toast.LENGTH_LONG);


}


}


get the notification service by calling as follows


notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

after clicking the notification you want to launch the another activity for that we need to create the Intent
like 
Intent launcIntent = new Intent(getApplicationContext(),NOtifiAct.class);


create the PendingIntent whch is capable for holoding the activity to be launched when click on the notification


PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, launcIntent, 0);


now create the notification object as forllows by passing the pendingIntent objec
and 
set the notification sound and lights when notification has occurred 


Notification notification = new Notification(R.drawable.icon, "SomethingHappend", System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "We r Finished", "Click Here", pendingIntent);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags    |= Notification.FLAG_AUTO_CANCEL;


by using the notify method on notificationManager object we can push the notification here we need to pass unique Integer ID and notification Object to the notify method


 notificationManager.notify(1, notification);


here is the Total code


main Activity i,e 




public class NotificationExActivity extends Activity implements OnClickListener{
Button notificationBtn;
NotificationManager notificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notificationBtn = (Button)findViewById(R.id.notificationBtn);
notificationBtn.setOnClickListener(this);  
}

Handler handler = new Handler();  

Runnable runnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent launcIntent = new Intent(getApplicationContext(),NOtifiAct.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, launcIntent, 0);
Notification notification = new Notification(R.drawable.icon, "SomethingHappend", System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "We r Finished", "Click Here", pendingIntent);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags    |= Notification.FLAG_AUTO_CANCEL;
           
            notificationManager.notify(1, notification);
}
};

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==notificationBtn){
handler.postDelayed(runnable, 10000);
Toast.makeText(getApplicationContext(), "NOtification Will post in 10 sec", Toast.LENGTH_LONG);

}

}

}

the activity to be launched when click on the notification


public class NOtifiAct extends Activity{
 @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
   TextView textView=new TextView(this);
   textView.setText("This Activity is Launched from the Notification Bar");
   setContentView(textView);
 }
}


some important flags are
FLAG_INSISTENT
the sound will be repeated until the notification is cancelled or notificaton bar is opened

 FLAG_NO_CLEAR  if it is set notification shold not clear when the user click on the CLEAR ALL button


FLAG_AUTO_CANCEL if u set this notification is cancelled when it is clicked by the user

           
                                     I HOPE NOW U GUYS  HAVE A BETTER UNDERSTANDING ON THE NOTIFICATION















Monday 16 April 2012

Iterator Vs ListIterator

Basically there are two differences 


1) Iterator is used to traverse the Set, List and Map where as ListIterator is used for only List type Objects


2) By Using Iterator we can We can traverse the Objects in One Direction (i,e Forward direction only using next() Metod) , By Using ListIterator we can traverse the Objects in BiDirectional i,e (forward as well as backward) by using  prnext() andevious() Methods


and 


Iterator is Base class for ListIterator i,e ListIterator extends Iterator 

Metods in Iterator and ListIterator


  1.     ListIterator Methods 

  2.    1) hasNext()

  3.    2) next()

  4.   3) remove()

  5.    4) hasPrevious()
  6.    
  7.    5) previous()

  8.    6) nextIndex()
            Iterator Methods
  1. next()

  2. hasNext()

  3. remove()



and One More Important thing is
by using the ListIterator we can modify the list when run time i,e When we are traversing the List


http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ListIterator.html

Example:

ArrayList<String> arl = new ArrayList<String>;
arl.add("1");
arl.add("2");
arl.add("3");
arl.add("4");
arl.add("5");


//Iterating the list using ListIterator
//forward direction


ListIterator itr = arl.listIterator();


while(itr.hasNext())
   System.out.println(itr.next());


//reverse direction



while(itr.hasPrevious())
   System.out.println(itr.previous());