Android Development (--notification) detailed and use

Source: Internet
Author: User

Notification is the meaning of the notice, Andong refers to the notice bar, generally used in the telephone, sms, mail, Alarm bell, in the mobile phone status bar will appear a small icon, prompting users to deal with this newsletter, when the hand from the top sliding status bar can be expanded and processed this newsletter.

In the help document, it is said that the notification class represents a persistent notification that will be submitted to the user using Notificationmanager. Added Notification.builder to make it easier to build notifications.

Notification is an alert user that lets your application run without opening or running in the background. It is an invisible program component (broadcast Receiver,service and inactive activity) that alerts users to the best way to take notice of events. first, distinguish between the following status bars and the status bar:

1, the status bar is the top of the mobile phone screen a shape of the region;
In the status bar has a lot of information: such as USB connection icon, cell phone signal icon, battery power icon, time icon and so on;


2, the status bar is the hand from the state bar sliding down the view can be telescopic;

There are generally two classes in the status bar (using the flag_ tag):

(1) the procedure under way; (2) is the notification event;


a notification transmission of information is: 1, a status bar icon;
2, in the Stretched Status bar window displays with the big title, the small title, the icon information, and has handles the Click event: for instance calls the program the entry class; 3, Flash, LED, or vibration;
under the face of some of the constants in the notification class, the field, a simple introduction: constants:
Default_all uses all default values, such as sounds, vibrations, splash screens, and so on Default_lights use the default flash hint Default_sounds Use default hint sound default_vibrate use default phone vibration
Note: When adding mobile phone vibration effect must be in the project list file to add mobile Vibration permission:

<uses-permission android:name= "Android.permission.VIBRATE"/>

The following is a simple example of a small amount to achieve the notification function: Mainactivity.java
Package Com.example.lession16_notifi;
Import android.app.Activity;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import android.content.res.Resources.NotFoundException;
Import Android.os.Bundle;
Import Android.view.View;

Import Android.widget.Toast;

	public class Mainactivity extends activity {private Notificationmanager Notificationmanager;
		@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
		Setcontentview (R.layout.activity_main); The first step: The Notificationmanager object is obtained by Getsystemservice () method, Notificationmanager = (Notificationmanager) getsystemservice (
	Notification_service); //Test public void Test1 (View v) {shownotification ("SMS", "5557", "Hello.")

	", R.drawable.ic_launcher, R.drawable.ic_launcher); //Step Two: Set some properties of notification such as: content, icon, title, corresponding notification action to handle, etc. public void shownotification (String tickertext, String Contenttitle, String ContentText, int iconID, int notiid) {//Create a Notification Notification Notification = new Notification ();
		Set notification message icon Notification.icon = iconID;
		Set the content that emits the message Notification.tickertext = Tickertext;

		Set the time to send notification Notification.when = System.currenttimemillis ();  Sets the default sound, vibration, and light effects when a notification is displayed notification.defaults = notification.default_vibrate;//vibration//Notification notification =

		New Notification (R.drawable.ic_launcher, "News", System.currenttimemillis ());
		Step 3: The Pendingintent Android system is responsible for maintaining pendingintent pendingintent = Pendingintent.getactivity (this, 0,getintent (), 0);

		4 steps: Set more detailed information Notification.setlatesteventinfo (this, contenttitle, contenttext,pendingintent); Step 5: Use the Notify method of the Notificationmanager object to display the notification message need to develop//notification identification Notificationmanager.notify (NOTIID, no

	Tification); //6 Step: Use the Cancelall () method of the Notificationmanager object to cancel the public void Clearnoti (View v) {notificationmanager.cancelall ();//Clear
 All}}

Layout file Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:pa" ddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" a ndroid:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparentright= "True" android:layout_alignparenttop= "true" android:layout_margintop= 22dp "android:onclick=" test1 "Android:text=" @string/text_notifi "/> <button android:id=" @+id/button2 "android:layou T_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/button1" android:layout_below= "@+id/button1" android:layout_margintop= "60DP" android:onclick= "Clearnoti" android:text= "@string/text_clear"/>
 ;/relativelayout>
Layout Effect Remember to implement the vibration effect add permissions to the manifest file:
<uses-permission android:name= "Android.permission.VIBRATE"/>
The implementation effect is as follows:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.