Android Notification status bar notification

Source: Internet
Author: User

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

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.