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