13.NOTIFICATION
13.1 ALL ABOUT NOTIFICATION
Several
types of situations may arise that require you to notify the user about an
event that occurs in your application. Some events require the user to respond
and others do not. For example:
·
When an event such as saving a file is
complete, a small message should appear to confirm that the save was
successful.
·
If the application is running in the
background and needs the user's attention, the application should create a
notification that allows the user to respond at his or her convenience.
·
If the application is performing work that
the user must wait for (such as loading a file), the application should show a
hovering progress wheel or bar.
Each of
these notification tasks can be achieved using a different technique:
·
A Status Bar Notification, for persistent reminders that come from the background and request the
user's response.
·
A Dialog
Notification, for Activity-related notifications (which
you just went through in last chapter)
Let’s go
through each of these
13.2 TOAST
A toast notification is a message that pops
up on the surface of the window. It only fills the amount of space required for
the message and the user's current activity remains visible and interactive.
The notification automatically fades in and out, and does not accept
interaction events. First, instantiate a Toast
object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast. It returns a
properly initialized Toast object. You can display the toast notification with
method
show().
13.3 STATUS BAR NOTIFICATION
You must be very similar with
screenshots like these.They can be classified by how they notify us i.e in four
different ways, one by providing a status bar notification as below, two by
flashing lights, three by vibration(which is not quite possible in emulator)
and finally by playing a sound.
A status bar notification should be used for
any case in which a background service needs to alert the user about an event
that requires a response. A background service should never launch an activity on its own
in order to receive user interaction. The service should instead create a
status bar notification that will launch the activity when selected by the
user.
An Activity or Service can initiate a status bar notification. Because an activity can perform
actions only while it is running in the foreground and its window has focus,
you will usually create status bar notifications from a service. This way, the
notification can be created from the background, while the user is using
another application or while the device is asleep. To create a notification,
you must use two classes: Notification and NotificationManager.
Use an
instance of the Notification class to define the properties of your status bar notification, such as
the status bar icon, the notification message, and extra settings such as a
sound to play. The NotificationManager is an Android system service that executes and manages all status bar
notifications. You do not instantiate the NotificationManager directly. In order to give it your Notification, you must retrieve a reference to the NotificationManager with getSystemService() and then, when you want to notify the user, pass it your Notification with notify().
To create a status bar notification:
- Get a reference to the NotificationManager:
private NotificationManager notifymanager;
|
final Notification notifyDetails = new Notification(R.drawable.ic_launcher,"CLICKED", System.currentTimeMillis());
|
Context context = getApplicationContext();
CharSequence contenttitle = "Notify
Details";
CharSequence contenttext = "Browse
for android by clicking me";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
PendingIntent pin = PendingIntent.getActivity(context, 0,
intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contenttitle,
contenttext, pin);
|
private int id;
notifymanager.notify(id, notifyDetails);
|
Complete code is given below , in our example we create a notification
by just a button click and removes it by another button.
package a.b.c;
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.net.Uri;
import android.os.Bundle;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class
NotificationActivity extends Activity {
Button b1,b2;
private int id = 1;
private NotificationManager notifymanager;
final Notification notifyDetails = new Notification(R.drawable.ic_launcher,"CLICKED", System.currentTimeMillis());
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.bttn1);
b2 = (Button)findViewById(R.id.bttn2);
notifymanager =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
b1.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View arg0) {
// TODO Auto-generated method stub
Context
context = getApplicationContext();
CharSequence
contenttitle = "Notify
Details";
CharSequence
contenttext = "Browse for android
by clicking me";
Intent
intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
PendingIntent pin = PendingIntent.getActivity(context,
0, intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contenttitle, contenttext, pin);
notifymanager.notify(id, notifyDetails);
Toast.makeText(getApplicationContext(),
"DONE.....", Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new
OnClickListener() {
@Override
public void
onClick(View arg0) {
// TODO Auto-generated method stub
notifymanager.cancel(id);
}
});
}
}
|
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bttn1"
android:text="Go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bttn2"
android:text="Again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
|