Before the online view of the custom message bar, notification bar, understand the notification This control, found that the UC browser is this type, today write a demo implementation,
Each of these buttons has different functions, the code is as follows:
Package Com.example.textwsjdemo;import Android.app.activity;import Android.app.notification;import Android.app.notificationmanager;import Android.app.pendingintent;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.view.keyevent;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.remoteviews;import Android.widget.toast;public class Mainactivity extends Activity {private Button bt_hehe;private Notificationmanager notificationmanager;private Notification notification;private int icon;private charsequence tickertext;private long when; Remoteviews contentview;private Intent intent;private pendingintent pendingintent;private int notification_id = 0; Private Mybroadcast receiver;private static String ACTION = "a"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); receiver = new Mybroadcast (); intentfilter filter = new Intentfilter (); Filter.addaction ("a"), Filter.addaction ("B"), Filter.addaction ("C"); Filter.addaction ("D"); Registerreceiver ( receiver, filter); Initview (); InitData ();} private void InitData () {icon = R.drawable.ic_launcher;//notification icon Tickertext = "Hello";//status bar display notification text prompt when = System.currentt Imemillis (); The time the notification is generated will be displayed in the notification message}private void Initview () {bt_hehe = (Button) Findviewbyid (r.id.bt_hehe); bt_ Hehe.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub/ /start the cue bar createnotification ();});} private void Createnotification () {Notificationmanager = (Notificationmanager) getsystemservice (context.notification _service); notification = new notification (); Notification.icon = Icon;notification.tickertext = Tickertext; Notification.when = when;/*** * Here we use a custom view to display notification */contentview = new Remoteviews (Getpackagename (), R.layout.notification_item); Contentview.settextviewtext (R.id.text11, "novel"); Contentview.settextviewtext (r.id.text22, "video"); Contentview.settextviewtext (R.id.text33, "News"); Contentview.settextviewtext (R.id.text44, "RIP");// Contentview.settextviewtext (r.id.notificationpercent, "0%");//Contentview.setprogressbar ( R.id.notificationprogress, 0, false);////progress bar//Contentview.setimageviewresource (r.id.image,r.drawable.more_ Advice)////Load Picture//Contentview.setimageviewresource (r.id.image,r.drawable.more_attention);// Contentview.setimageviewresource (r.id.image,r.drawable.more_evaluate);//Contentview.setimageviewresource ( R.id.image,r.drawable.more_about);//Contentview.settextviewtext (R.id.text, "hello,this message is in a custom Expanded view ");///Text notification.flags = notification.flag_ongoing_event; Set resident, cannot swipe cancel//default Jump Main interface Intent = new Intent (this, mainactivity.class); Intent.addflags (Intent.flag_activity_single_ TOP);p endingintent = pendingintent.getactivity (this, 0, intent, 0);//Custom Jump Contentview.setonclickpendingiNtent (R.id.ll_11, Pendingintent.getbroadcast (Mainactivity.this, one, New Intent (). Setaction ("a"), Pendingintent.flag _update_current)); Contentview.setonclickpendingintent (r.id.ll_22, Pendingintent.getbroadcast (MainActivity.this, One, New Intent (). Setaction ("B"), pendingintent.flag_update_current); Contentview.setonclickpendingintent (R.id.ll_ Pendingintent.getbroadcast (Mainactivity.this, one, New Intent () setaction ("C"), Pendingintent.flag_update_ (current)); Contentview.setonclickpendingintent (r.id.ll_44, Pendingintent.getbroadcast (Mainactivity.this, one, new Intent (). Setaction ("D"), pendingintent.flag_update_current)); notification.contentview = Contentview; Notification.contentintent = pendingintent;notificationmanager.notify (notification_id, notification);} Cancellation Notice private void Cancelnotification () {Notificationmanager.cancelall ();} @Overrideprotected void OnDestroy () {cancelnotification (); Unregisterreceiver (receiver);} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (KeycoDe = = Keyevent.keycode_back) {System.out.println ("Press back Key OnKeyDown ()"); Cancelnotification ();} Return Super.onkeydown (KeyCode, event);} Class Mybroadcast extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {if ( Intent.getaction (). Equals ("a")) {Toast.maketext (mainactivity.this, "11111111111111", Toast.length_long). Show (); StartActivity (New Intent (Mainactivity.this, Activitytext1.class));} if (Intent.getaction (). Equals ("B")) {Toast.maketext (mainactivity.this, "222222222222222", Toast.length_long). Show ( ); StartActivity (New Intent (Mainactivity.this, Activitytext2.class));} if (Intent.getaction (). Equals ("C")) {Toast.maketext (mainactivity.this, "333333333333", Toast.length_long). Show (); StartActivity (New Intent (Mainactivity.this, Activitytext3.class));} if (Intent.getaction (). Equals ("D")) {Toast.maketext (mainactivity.this, "4444444444444", Toast.length_long). Show (); StartActivity (New Intent (Mainactivity.this, Activitytext4.class));}}}
The following are the settings for some properties:
/** Add sound * notification.defaults |=notification.default_sound;* or use the following methods * Notification.sound = Uri.parse ("file:/// Sdcard/notification/ringer.mp3 "); * Notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI," 6 ") ) * If you want the sound to continue repeating until the user responds to the notification, you can add "flag_insistent" to the Flags field in notification if notification defaults field includes Default_ Sound property, this property overrides the sounds defined in the Voice field *//** add Vibration * Notification.defaults |= notification.default_vibrate;* or you can define your own vibration mode: * Long[] vibrate = {0,100,200,300}; Vibrates after 0 milliseconds, vibrates after 100 milliseconds, and vibrates again after 200 milliseconds for 300 milliseconds * notification.vibrate = vibrate;* Long array can be defined as any length desired * If notification's defaults field includes "Default_vibrate", this property overrides the vibration *//** defined in the Vibrate field to add an LED light alert * Notification.defaults |= Notification.default_lights;* or can own LED reminder mode: * Notification.ledargb = 0xff00ff00;* Notification.ledonms = 300; Time of light * notification.ledoffms = 1000; Time to Extinguish * notification.flags |= notification.flag_show_lights;*//** more characteristic properties * notification.flags |= FLAG_AUTO_CANCEL; Automatically clear this notification after clicking this notification on the notification bar * Notification.flags |= FLAG_insistent; Repeat the sound until the user responds to this notification * notification.flags |= flag_ongoing_event; Place this notice in the "ongoing" of the notification bar, which is the "running" group * notification.flags |= Flag_no_clear; Indicates that this notification is not cleared after clicking "Clear Notification" in the notification bar, *//often used with flag_ongoing_event * notification.number = 1; The number field represents the current amount of events represented by this notification, which will be overwritten at the top of the status bar icon *//If you want to use this field, you must start with 1 * notification.iconlevel =; //*/
Source code Download: Click to download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android High-imitation UC browser and 360 mobile defender Message Resident column (notification bar)