Android learning notes (1) Notification

Source: Internet
Author: User

First, create an Android Project

Edit main. xml

The Code is as follows:

[Xhtml]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/send"
Android: id = "@ + id/button"
/>
</LinearLayout>

Edit strings. xml

[Xhtml]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> notification </string>
<String name = "app_name"> Status Bar notification </string>
<String name = "send"> send notification </string>
</Resources>

Then process the Notification.

 

The following describes how to create and display a Notification. To create and display a Notification, follow these steps:

1. Get a icationicationmanager object through the getSystemService method.

2. Create a Notification object. Each Notification corresponds to a Notification object. In this step, you need to set the notification message displayed on the status bar above the screen, the image resource ID in front of the notification message, and the time when the notification is sent. It is generally the current time.

3. Notification can be detached from the application. That is to say, even if the application is closed, Notification will still be displayed in the status bar. After the application is started again, you can re-control these notifications. Such as clearing or replacing them. Therefore, you must create a PendingIntent object. This object is maintained by the Android system. Therefore, the object will not be released after the application is closed.

4. Use the setLatestEventInfo method of the Notification class to set Notification details.

5. Use the y icationmanager class's notify y method to display Notification messages. In this step, you must specify the unique ID that identifies the Notification. This ID must be unique relative to the same NotificationManager object. Otherwise, it will overwrite the same icationicaiton object.

It's not as exciting as action. Let's demonstrate how to display a Notification in the status bar. The Code is as follows:

 

[Java]
Package com. cayden. notification;
 
Import android. app. Activity;
Import android. app. Notification;
Import android. app. icationicationmanager;
Import android. app. PendingIntent;
Import android. content. Context;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
/**
* @ Author Cui ran
*
*/
Public class icationicationactivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button button = (Button) this. findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
/**
* Get notification Manager
*/
Icationicationmanager NotificationManager = (notificationManager) getSystemService (Context. icationication_service );
Int icon = android. R. drawable. sym_action_email;
Long when = System. currentTimeMillis ();
/**
* Create a notification icon and title
*/
Notification notification = new Notification (icon, null, when );
Notification. defaults = Notification. DEFAULT_SOUND; // send the default sound
PendingIntent contenIntent = PendingIntent. getActivity (icationicationactivity. this, 0, null, 0 );
Notification. setLatestEventInfo (icationicationactivity. this, "meeting notification", "The meeting will be held at half past four P.M. this afternoon at 512! ", ContenIntent );


Icationicationmanager. Y (0, notification); // send a notification

}
});
}
}

 

Running effect:

 

 

 

Click send notification.

Effect

 

 

 

 

Add an email symbol in the upper left corner.

Drag down with the mouse selected

 

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.