Learn Android from scratch--Notification notifications

Source: Internet
Author: User

Look at the effect

The layout is so simple, I'm not going to put it on it. I'll show you the core code. int Notificationid = 1;

//set the intent after clicking NotificationsIntent Intent =NewIntent ( This, Notificationview.class); Intent.putextra ("Notificationid", Notificationid); //plug-in pendingintent parameter explanation: 1. Context 2. Request code (Request code for Intent) 3. Intent (to initiate the intent of the target activity) 4. Flag (flag used when activity starts )Pendingintent pendingintent = pendingintent.getactivity ( This, 0, intent,pendingintent.flag_cancel_current); //get notification Management services for your systemNotificationmanager Manager =(Notificationmanager) Getsystemservice (Notification_service); //It is now officially recommended to build notification in the form of builder, which is the new way of being obsolete. Notification.builder Builder =NewNotification.builder ( This); //The information that appears at the top when you receive a notificationBuilder.setsmallicon (R.mipmap.ic_launcher); Builder.setticker ("Reminder:meeting starts in 5 minutes");        Builder.setwhen (System.currenttimemillis ()); //Drop -down top See the detailed notification information icon CommonBuilder.setcontenttitle ("System Alarm");//Set TitleBuilder.setcontenttext ("meeting with customer at 3pm..");//Set Content//Setting the intent of the notification to be clicked is to open an activity//As for why use Pendingintent, because the Pendingintent object can help you perform an action at some later time on behalf of the application, "regardless of whether the application is running"builder.setcontentintent (pendingintent); Builder.setdefaults (Notification.default_sound); //Set the default sound and vibration//Of course you can also customize the set of vibrations//builder.setvibrate (New long[]{100,250,100,500});//Set VibrationBuilder.setautocancel (true);//set the click to disappear automatically, that is, to cancel the display true: click to disappear; false: not disappear after clicking; false by defaultNotification Notif = Builder.build ();//Build here Note build method minimum support MinsdkversionManager.notify (notificationid,notif);//Notification Management Service sends notifications you build you'll get a notification at this point

Note the inside of the Builder.setautocancel (true); If you do not want the program to automatically help you click and close, but your own code in another location to close, then you can not write this code, you can set to False, and then close the code can write

        // access to services        Notificationmanager nm = (Notificationmanager) Getsystemservice (notification_service);        Nm.cancel (Getintent (). Getintextra (// turn off notifications showing Notificationid to 1

Remember to close the ID that matches your notification. Study hard, day after day, so much more.

Study record, if wrong please point out, thank you!

Learn Android from scratch--Notification notifications

Related Article

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.