Basic usage of Android notifications

Source: Internet
Author: User

Basic usage of creating a new Android Project notification bar

Modify the code for the Activity_main.xml as follows:

<Relativelayoutxmlns: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:paddingbottom= "@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 " >    <ButtonAndroid:id= "@+id/send_notice"Android:onclick= "click"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send notice" /></Relativelayout>

The layout file is very simple and there is only one send Notice button for sending a notification.

Create an activity interface and need to prepare a new layout file, Notification_layout.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centerinparent= "true"Android:text= "This is notification layout"android:textsize= "24SP" /></Relativelayout>

The new notificationactivity inherits the activity and loads the layout file just defined here.

 PackageCom.example.tongzhi;Importandroid.app.Activity;ImportAndroid.app.NotificationManager;ImportAndroid.os.Bundle; Public classNotificationactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //set the interface for clicking on the notification bar informationSetcontentview (r.layout.notification_layout); //to disappear information from the notification barNotificationmanager Manager =(Notificationmanager) Getsystemservice (Notification_service); Manager.cancel (1); }}

Next, modify the code in the Mainactivity

 PackageCom.example.tongzhi;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {Switch(View.getid ()) { CaseR.id.send_notice:notificationmanager Manager=(Notificationmanager) Getsystemservice (Notification_service); //The third parameter specifies when the notification information is createdNotification Notification =NewNotification (R.drawable.ic_launcher,"This is ticker text", System.currenttimemillis ()); //response to a click notification message, PendingintentIntent Intent =NewIntent ( This, Notificationactivity.class); Pendingintent Pi= Pendingintent.getactivity ( This, 0, intent, pendingintent.flag_cancel_current); //To set the layout of the notification, the following method can set a standard layout for the notificationNotification.setlatesteventinfo ( This, "This is content title",                    "This is content text", PI); //Call Notificationmanager's Notify () method to make the notification appearManager.notify (1, notification);  Break; default:             Break; }    }}

Basic usage of Android notifications

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.