Android-notification Notification Demo

Source: Internet
Author: User

Code

Activity01

 Packagecom.lxt008;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classActivity01extendsactivity{Button m_button1, M_button2, M_button3, M_button4; //claims Notification (message) ManagerNotificationmanager M_notificationmanager;    Intent m_intent;    Pendingintent m_pendingintent; //declaring notification ObjectsNotification m_notification; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //initializing the Notificationmanager objectM_notificationmanager =(Notificationmanager) Getsystemservice (Notification_service); //Get 4 button ObjectsM_button1 =(Button) Findviewbyid (R.ID.BUTTON01); M_button2=(Button) Findviewbyid (R.ID.BUTTON02); M_button3=(Button) Findviewbyid (R.ID.BUTTON03); M_button4=(Button) Findviewbyid (r.id.button04); //Transfer content When you click NotificationsM_intent =NewIntent (Activity01. This, Activity02.class); //The main is to set the class to display content when clicking on notificationsM_pendingintent = Pendingintent.getactivity (Activity01. This, 0, m_intent, 0); //Constructing notification ObjectsM_notification =NewNotification (); M_button1.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {//set the icon that the notification displays in the status barM_notification.icon =R.DRAWABLE.IMG1; //What is displayed when we click on the notificationM_notification.tickertext = "Button1 notification content ....."; //make a default sound when notifiedM_notification.defaults =Notification.default_sound; //set parameters for notification displayM_notification.setlatesteventinfo (Activity01. This, "Button1", "Button1 notice", m_pendingintent); //can be understood as executing this notificationM_notificationmanager.notify (0, m_notification);        }        }); M_button2.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {M_notification.icon=R.drawable.img2; M_notification.tickertext= "Button2 notification content ....."; //Vibration at NotificationM_notification.defaults =notification.default_vibrate; M_notification.setlatesteventinfo (Activity01. This, "Button2", "Button2 notice", m_pendingintent); M_notificationmanager.notify (0, m_notification);        }        }); M_button3.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {M_notification.icon=R.DRAWABLE.IMG3; M_notification.tickertext= "Button3 notification content ....."; //The screen glows when notifiedM_notification.defaults =notification.default_lights; M_notification.setlatesteventinfo (Activity01. This, "Button3", "Button3 notice", m_pendingintent); M_notificationmanager.notify (0, m_notification);        }        }); M_button4.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {M_notification.icon=R.drawable.img4; M_notification.tickertext= "BUTTON4 notification content ...."; //the notification is both vibrating and the screen is lit and the default soundM_notification.defaults =Notification.default_all; M_notification.setlatesteventinfo (Activity01. This, "Button4", "Button4 notice", m_pendingintent); M_notificationmanager.notify (0, m_notification);    }        }); }}

Activity02

 Package com.lxt008; Import android.app.Activity; Import Android.os.Bundle;  Public class extends activity{    publicvoid  onCreate (Bundle savedinstancestate)    {         Super. OnCreate (savedinstancestate);         // there is a direct limit to a TextView         Setcontentview (r.layout.main2);    }}

Layout file

Main

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    >    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "0"Android:paddingbottom= "4dip"        />    <ButtonAndroid:id= "@+id/button01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Button01"        >        <Requestfocus/>    </Button>    <ButtonAndroid:id= "@+id/button02"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Button02"        >    </Button>    <ButtonAndroid:id= "@+id/button03"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Button03"        >    </Button>    <ButtonAndroid:id= "@+id/button04"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Button04"        >    </Button></LinearLayout>

Main2

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    >    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Thank you for using!" "    /></LinearLayout>

Android-notification Notification Demo

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.