Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305
This article resources with the help of netizens: Phantom Prodigal
-topic: Notification of user interface (notification bar)
-
Notification instance (with system self-notification+ semi-custom notification+ custom notification)
Activity_main.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:layout_width =" fill_parent " android:layout_height =" fill_parent " android:orientation =; <button android:id = "@+id/le10bt01" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text =" basenotification "/> <button android:id = "@+id/le10bt02" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text =" updatebasenotification "/> <button android:id = "@+id/le10bt03" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text =" clearbasenotification "/> <button android:id = "@+id/le10bt04" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text =" medianotification "/> <button android:id = "@+id/le10bt05" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text =" clearmedianotification "/> <button android:id = "@+id/le10bt06" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text =" ClearALL "/> <buttonandroid:id="@+id/le10bt07"android:layout_width="Fill_ Parent "android:layout_height="wrap_content "android:text=" CustomNotification " /> </linearlayout>
Mainactivity.java:
PackageCom.example.demo2;Importandroid.app.Activity;ImportAndroid.app.Notification;ImportAndroid.app.NotificationManager;ImportAndroid.app.PendingIntent;ImportAndroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.provider.MediaStore.Audio;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.RemoteViews; Public class mainactivity extends Activity { PrivateNotificationmanager nm;PrivatePendingintent PD;PrivateButton bt01,bt02,bt03,bt04,bt05,bt06,bt07,bt08;@Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Init (); }Private void Init() {bt01 = (Button) Findviewbyid (r.id.le10bt01); bt02 = (Button) Findviewbyid (r.id.le10bt02); bt03 = (Button) Findviewbyid (r.id.le10bt03); bt04 = (Button) Findviewbyid (r.id.le10bt04); bt05 = (Button) Findviewbyid (r.id.le10bt05); bt06 = (Button) Findviewbyid (r.id.le10bt06); bt07 = (Button) Findviewbyid (r.id.le10bt07); Bt01.setonclicklistener (onclick); Bt02.setonclicklistener (onclick); Bt03.setonclicklistener (onclick); Bt04.setonclicklistener (onclick); Bt05.setonclicklistener (onclick); Bt06.setonclicklistener (onclick); Bt07.setonclicklistener (onclick); NM = (Notificationmanager) getsystemservice (Notification_service); Intent Intent =NewIntent ( This, Mainactivity.class); PD = Pendingintent.getactivity (mainactivity. This,0, Intent,0); } Onclicklistener onclick =NewOnclicklistener () {//base Notification ID Private intNotification_id_base =1;PrivateNotification Basenf;//medianotification ID Private intNotification_id_media =2;PrivateNotification medianf;//customnotification ID Private intNotification_id_custom =3;PrivateNotification CUSTOMNF;@Override Public void OnClick(View v) {Switch(V.getid ()) { CaseR.ID.LE10BT01://new status bar notificationBasenf =NewNotification ();//Set the icon that the notification displays in the status barBasenf.icon = R.drawable.myimage;//notification when the status bar displays the contentBasenf.tickertext ="You clicked Basenf!";//Notifications Show time in status barBasenf.when = System.currenttimemillis ();//Use the default hereBasenf.defaults |= Notification.default_sound; Basenf.defaults |= notification.default_vibrate; Basenf.defaults |= notification.default_lights;//Let the sound, vibration infinite loop, until the user respondsBasenf.flags |= notification.flag_insistent;//notifications are clicked and disappear automaticallyBasenf.flags |= Notification.flag_auto_cancel;//Click ' Clear ', not clear the notice (QQ notice can not be cleared, it is used this)Basenf.flags |= notification.flag_no_clear;///second parameter: Message title expanded when the drop-down status bar is displayed ///Third parameter: message content displayed when drop-down status bar expanded message text //Fourth parameter: Perform a page jump when you click on the notificationBasenf.setlatesteventinfo (mainactivity. This,"TITLE01","Content01", PD);//Issue status bar notificationsNm.notify (Notification_id_base, BASENF); Break; CaseR.ID.LE10BT02://Update notifications //For example, the status bar prompt has a new text message, haven't had time to check, and a new text message prompt. //At this time using the method of updating the original notification comparison. //(it is also possible to resend a notification, but this will cause confusion in the notification, and display multiple notifications to the user, also unfriendly to the user)Basenf.setlatesteventinfo (mainactivity. This,"TITLE02","Content02", PD); Nm.notify (Notification_id_base, BASENF); Break; Caser.id.le10bt03://Clear BasenfNm.cancel (notification_id_base); Break; CaseR.ID.LE10BT04:MEDIANF =NewNotification (); Medianf.icon = R.drawable.myimage; Medianf.tickertext ="You clicked Medianf!"; Medianf.when = System.currenttimemillis ();//Custom SoundMedianf.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI,"6");//vibration emitted at the time of notification ///First parameter: Time to wait before vibration ///second parameter: Length of first vibration, etc. Long[] vir = {0, -, $, -}; Medianf.vibrate = vir; Medianf.setlatesteventinfo (mainactivity. This,"Title03","Content03", PD); Nm.notify (Notification_id_media, medianf); Break; Caser.id.le10bt05://Clear medianfNm.cancel (Notification_id_media); Break; CaseR.id.le10bt06:nm.cancelall (); Break; Caser.id.le10bt07://Customize the drop-down view, such as the progress bar shown when downloading the software. CUSTOMNF =NewNotification (); Customnf.icon = R.drawable.myimage; Customnf.tickertext ="custom!"; Remoteviews Contentview =NewRemoteviews (Getpackagename (), r.layout.custom); Contentview.setimageviewresource (R.id.image, r.drawable.myimage); Contentview.settextviewtext (R.id.text,"Hello, this message was in a custom expanded view"); Customnf.contentview = Contentview;//When using a custom drop-down view, you do not need to call the Setlatesteventinfo () method again //But contentintent must be definedCustomnf.contentintent = PD; Nm.notify (Notification_id_custom, CUSTOMNF); Break; } } }; }
Custom Notification Layout Custom.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:orientation =" horizontal " android:layout_width =" fill_parent " android:layout_height =" fill_parent " android:padding =; <imageview android:id = "@+id/image" android:layout_width= "wrap_content" android:layout_height = "fill_parent" android:layout_marginright= "10DP" /> <TextView android:id= "@+id/text"android:layout_width="Wrap_ Content "android:layout_height=" Fill_parent "android:textcolor=" #000 " /> </linearlayout>
Operation Result:
Notification Bar:
Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305
Step by Step _android Development Course [21]_ User interface Notification (notification bar)