Android High-imitation UC browser and 360 mobile defender Message Resident column (notification bar)

Source: Internet
Author: User

Before the online look down on their own definition of the message bar, notification bar, learned that the notification this control. Discover the UC browser and so on are such types, today write a demo implementation.


Each of these buttons has a different function. 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 up resident. Cannot swipe cancel//default Jump Main interface Intent = new Intent (this, mainactivity.class); Intent.addflags (intent.flag_activity_single_top); Pendingintent = pendingintent.getactivity (this, 0, intent, 0);//define 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_33, 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));}}}

Here are the settings for some properties:

/** Add sound * notification.defaults |=notification.default_sound;* or use several ways * Notification.sound = Uri.parse ("file:/// Sdcard/notification/ringer.mp3 "); * Notification.sound = Uri.withappendedpath (Audio.Media.INTERNAL_CONTENT_URI," 6 ") ); * False assumption to keep the voice repeating until the user responds to the notification, you can add "flag_insistent" to the Flags field of notification * Assuming notification defaults field contains "Default_ Sound property, this property overrides the sounds defined in the Voice field *//** add Vibration * notification.defaults |= notification.default_vibrate;* or be able to define your own vibrational 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 desired regardless of length * Assuming that the defaults field of notification contains "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;*//** Many other characteristic attributes * Notification.flags |= FLAG_AUTO_CANCEL; After clicking this notice on the notification bar, you voluntarily clear this notice * notification.flags|= flag_insistent; Make a sound repeatedly until the user responds to this notification * notification.flags |= flag_ongoing_event; Place this notice on the "ongoing" in the notification bar, which is the "executing" group * notification.flags |= Flag_no_clear; Indicates that after clicking Clear Notification in the notification bar. This notification is not cleared, *//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 *//assuming that you want to use this field, you must start at 1 * notification.iconlevel =; //*/

Source Download: Click to download


Android High-imitation UC browser and 360 mobile defender Message Resident column (notification bar)

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.