This example describes the notification usage in Android. Share to everyone for your reference, specific as follows:
Notification can be understood as the meaning of the notice is generally used to display broadcast information notification must be used to Notificationmanager
you typically have three steps to use notification, as shown below
① general access to system-level service Notificationmanager.
Call the Context.getsystemservice (Notification_service) method to return the Notificationmanager instance
② instantiate the notification and set its properties
Constructs a Notification instance with the Notification constructor public Notification (int icon, charsequence tickertext, Long)
③ through the Notificationmanager to send a notice on the OK
Notificationmanager has two methods: Notify () to send a notice to cancel (...) Cancel Notification
The following is a code example to introduce notification
Initialize Notificationmanager and notification two member variables first
Notificationmanager = (Notificationmanager) this.getsystemservice (notification_service);
notification = new Notification (R.drawable.touxiang, "info", System.currenttimemillis ());
The parameters that are passed in the Ps:notification constructor are as follows the first argument is the image, the second is the title: information, and the third is the system time (see figure I) The reason for this constructor is to think of the following setlatesteventinfo (...) Make a difference.
(Figure I)
(Figure II)
The layout is no more than two button. Look at the Send button
Sendbutton.setonclicklistener (New Onclicklistener ()
{
@Override public
void OnClick (View v)
{
Intent Intent = new Intent (notificationactivity.this,notificationactivity.class);
Pendingintent pendingintent = pendingintent.getactivity (notificationactivity.this, 0, intent, 0);
Notification.setlatesteventinfo (Notificationactivity.this, "One piece of information for You", "information from John", Pendingintent);
Notificationmanager.notify (id,notification);
Notificationmanager.notify (id+1, notification);
}
)
Setlatesteventinfo (...) The effect of the parameters in the picture: (see figure II) There are two identical messages in the notice, yes you are not mistaken, this is because I use Notificationmanager notify (notice) two times and the ID is different, ID is an int type is the identifier of the notification information. Although the above two messages are exactly the same, Android displays two messages because of the different IDs.
What do you mean by the role of the parameter Pendingintent in Setlatesteventinfo? Pendingintent can be performed in another place, not with immediate intent. When the user clicks on the extended notification, pendingintent intention begins to execute, such as Figure II when I click on one of the messages, I immediately enter another activity ...
As seen above, in addition to setting the icon for the notification, the title can also set the beep, vibration, flash details please see me turn to a piece of article notification use detailed ...
Package com.study.android;
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.view.View.OnClickListener;
Import Android.widget.Button;
public class Mainactivity extends activity {private Button startbtn;
Private Button cancelbtn;
private static final int hello_id = 1;
Notificationmanager Mnotificationmanager;
Notification mnotification; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
STARTBTN = (Button) Findviewbyid (R.ID.STARTBTN);
CANCELBTN = (Button) Findviewbyid (R.ID.CANCELBTN);
① gets the Notificationmanager reference String ns = Context.notification_service; Mnotificationmanager = (Notificationmanager) this.getsYstemservice (NS);
② initialization Notification int icon = R.drawable.ic_launcher;
Charsequence tickertext = "Hello";
Long when = System.currenttimemillis ();
Mnotification = new Notification (icon,tickertext,when);
Mnotification.defaults = Notification.default_all;
Mnotification.flags |= notification.flag_no_clear;
Mnotification.flags |= notification.flag_show_lights;
③ defines notification messages and pendingintent context = this;
Charsequence Contenttitle = "My Notification";
Charsequence contenttext = "Hello World";
Intent notificationintent = new Intent (this,mainactivity.class);
Pendingintent contentintent = pendingintent.getactivity (context, 0, notificationintent,0);
Mnotification.setlatesteventinfo (context, Contenttitle, ContentText, contentintent); ④ notification incoming Notificationmanager//Open notification Startbtn.setonclicklistener (new Onclicklistener () {@Ove Rride public void OnClick (View v) {MnotificatIonmanager.notify (hello_id,mnotification);
}
});
Cancel Notification Cancelbtn.setonclicklistener (new Onclicklistener () {@Override public void OnClick (View v) {
Mnotificationmanager.cancel (hello_id);
}
});
}
}
There are steps to create notification in your code, and you can look at the details.
More interested readers of Android-related content can view the site's topics: "Android View View tips Summary", "Android resource Operation Tips", "Android file Operation Tips", "Android operation SQLite Database Skills Summary", "Android operation JSON data technique summary", "Android Database Operation skills Summary", "Android programming SD Card Operation method Summary", "Android Development introduction and Advanced Course", " A summary of the operational skills of Android programming and the summary of the usage of Android controls
I hope this article will help you with the Android program.