Android Project Imitation UC browser and 360 Mobile Guardian message resident column (notification bar) _android

Source: Internet
Author: User

Before the internet looked at the custom message bar, notice bar, learned that notification this control, found that UC browsers are this type, today to write a demo implementation, as shown:

Each of these buttons has a different function, and 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"; @Override Protected 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";//the notification text prompts when the status bar displays = System.currenttimemillis (); 
  The time that 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 () {@Override public void OnClick (View v) {//TODO Auto-gen 
   erated method Stub//Start Prompt bar createnotification (); 
 } 
  }); private void Createnotification () {Notificationmanager = (Notificationmanager) getsystemservice (Context.notifica 
  Tion_service); Notification = new Notification (); 
  Notification.icon = icon; 
  Notification.tickertext = Tickertext; 
 
  Notification.when = when; /*** * Here we use custom view to show notification/Contentview = new Remoteviews (Getpackagename (), R.layout.notificati 
  On_item); 
  Contentview.settextviewtext (R.id.text11, "novel"); 
  Contentview.settextviewtext (r.id.text22, "video"); 
  Contentview.settextviewtext (R.id.text33, "News"); 
  Contentview.settextviewtext (R.id.text44, "nonsense"); 
  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 slide cancellation//default Jump Main Interface Intent = new Intent (this, mainactivity.class); 
  Intent.addflags (Intent.flag_activity_single_top); 
   
  Pendingintent = 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_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); 
 //Cancel notification private void Cancelnotification () {notificationmanager.cancelall (); 
  } @Override protected void OnDestroy () {cancelnotification (); 
 
 Unregisterreceiver (receiver); 
   @Override public boolean onKeyDown (int keycode, keyevent event) {if ((keycode = = Keyevent.keycode_back)) { 
   System.out.println ("Press the Back Key OnKeyDown ()"); 
  Cancelnotification (); 
 Return Super.onkeydown (KeyCode, event); Class Mybroadcast extends Broadcastreceiver {@Override public void onreceive (context context, Intent Inten T) {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 to repeat until the user responds to the notification, you can add "flag_insistent" to the Flags field in the Notification if notification defaults field includes "Default_sound" 
property, this property will overwrite the voice defined in the Sound field/* * Add Vibration * notification.defaults |= notification.default_vibrate; * or you can define your own vibrational mode: * long[] vibrate = {0,100,200,300}; 
0 milliseconds after the vibration, vibration 100 milliseconds after the stop, after 200 milliseconds again vibration 300 milliseconds * notification.vibrate = vibrate; * Long array can be defined as any length desired * If the notification defaults field includes "Default_vibrate", this property will overwrite the vibration defined in the vibrate field/* * Add LED lights reminder * Notif 
Ication.defaults |= notification.default_lights; 
* or can own LED reminder mode: * Notification.ledargb = 0xff00ff00; * Notification.ledonms = 300; Light time * notification.ledoffms = 1000; 
Extinguished time * notification.flags |= notification.flag_show_lights; * * * More feature attributes * Notification.flags |= flag_aUto_cancel; Click this notice on the notification bar to automatically clear this notice * 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", "Running" group, in the notification bar * Notification.flags |= flag_no_clear; Indicates that the notice is not cleared after clicking on the "purge notice" 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, and it will overwrite the top of the status bar icon *//If you want to use this field, you must start with 1 * notification.iconlevel =; 
 //

Finally attached source code: source Download

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.