Android Learning (20) Notification notification bar

Source: Internet
Author: User

First, the contents of the notification bar

1. Icons

2. Title

3. Content

4. Time

5, the corresponding after the click

Second, how to implement the notification bar

1, get Notificationmanager.

2, display the notification bar: Notify (id,notification);

3, the cancellation notice bar: cancle (ID);

4, constructs the notification and sets the display content;

5, notification bar notification can set the sound prompts, indicators, as well as vibration effect.

Iii. Sample code: Send notification and cancel notification

<LinearLayoutxmlns: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:orientation= "vertical"Tools:context=". Mainactivity ">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Use of the notification bar" />    <ButtonAndroid:id= "@+id/btn_send"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Send Notification"/>    <ButtonAndroid:id= "@+id/btn_cancel"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Cancel Notification"/></LinearLayout>

Add Permissions:

    <!-- the permission of the light  -    <  android:name= "Android.permission.FLASHLIGHT"/>    <!-- Vibrate Permissions  -    <  android:name= "Android.permission.VIBRATE"/>

Background code:

 Packagecom.example.zhengcheng.myapplication;Importandroid.app.Activity;ImportAndroid.app.NotificationManager;Importandroid.app.PendingIntent;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.app.Notification; Public classMainactivityextendsActivity {Button btn_send; //send buttonButton Btn_cancel;//Cancel ButtonNotificationmanager Manager;//notification Control class    intNotificationid; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //creating Notificationmanager through System servicesManager =(Notificationmanager) Getsystemservice (Context.notification_service); Btn_send=(Button) Findviewbyid (r.id.btn_send); Btn_send.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {sendnotication ();        }        }); Btn_cancel=(Button) Findviewbyid (r.id.btn_cancel); Btn_cancel.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {manager.cancel (Notificationid);    }        }); }    /*** Send notification information*/    Private voidsendnotication () {Intent Intent=NewIntent ( This, Mainactivity.class); Pendingintent pendingintent= Pendingintent.getactivity ( This, 0, intent, 0); Notification.builder Builder=NewNotification.builder ( This);   Builder.setsmallicon (R.mipmap.ic_launcher); //Set iconBuilder.setticker ("Mobile phone status bar Prompt");//Phone status bar tipsBuilder.setwhen (System.currenttimemillis ());//Set the timeBuilder.setcontenttitle ("Notification bar title");//set the title barBuilder.setcontenttext ("I Come from Notificationdemo");//set the contents of the notification barBuilder.setcontentintent (pendingintent);//set which activity pops up when you click the notification bar//builder.setdefaults (Notification.default_sound); //Set Prompt Sound//builder.setdefaults (notification.default_lights); //Setting the LED//builder.setdefaults (notification.default_vibrate); //Set Vibrationbuilder.setdefaults (Notification.default_all); //set all of the above effectsNotification Notification = Builder.build ();//Android4.1 more than the version,//Notification Notification = Builder.getnotification (); //if the version below 4.1 uses builder.getnotification ();manager.notify (notificationid, notification); }}

Android Learning (20) notification notification bar

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.