No additional vibration and sound effects are added, the function of notification is realized directly here, see the effect: Mainactivity.java
Package Com.example.notification;import Android.os.bundle;import Android.annotation.suppresslint;import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.context;import Android.content.intent;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Mainactivity extends Activity {public Notificationmanager mnotificationmanager; @SuppressLint ("Newapi") @ overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Button btn = (button) Findviewbyid (R.id.button1); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//1-Get a reference to Motificationmanager. String ns = Context.notification_service; Mnotificationmanager = (Notificationmanager) getsystemservice (NS);//2-instantiation notification:int icon = R.drawable.new_ Mail Charsequence tickertext = "Hello"; long when = System.currenttimemillis (); Notification Notification = new Notification (icon, tickertext,when);//3-Define Notification, such as show icon, target intent and other information context context = Getapplicationcontext (); Charsequence contenttitle = "My notification"; Charsequence contenttext = "Hello world!"; Ntent notificationintent = new Intent (V.getcontext (), messageactivity.class); Pendingintent contentintent = pendingintent.getactivity (V.getcontext (), 0, notificationintent, 0); Notification.setlatesteventinfo (context, Contenttitle,contenttext, contentintent);//4-pass to manager.final int HELLO_ ID = 1;mnotificationmanager.notify (hello_id, notification);});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}
Messageactivity.java
Package Com.example.notification;import Android.app.activity;import Android.app.notificationmanager;import Android.content.context;import Android.os.bundle;public class Messageactivity extends Activity {@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_message ); Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Context.notification_service); Mnotificationmanager.cancel (1);//This is achieved, after clicking on the message, the ability to clear the message on their own initiative. }}
The XML layout file is not written. Very easy ~ Here does not implement the notification when the arrival of the prompt effect, such as vibration, sound and so on. Will be in the ANDROID notification implementation of the push message to receive the message side of the voice and vibration and bright screen prompts the article to implement such a function
Android Notification status bar notification