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
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