Android learning diary 21-Toast and Notification of message prompts

Source: Internet
Author: User

1. Toast is similar to sliced bread. It is used to pop up quick prompt information. When Toast is displayed, although it suspends the top of the application, it does not get the focus. It is designed to prompt useful information without interrupting other operations. Ease of use: 1 // The first parameter: the current context. GetApplicationContext () or this 2 // second parameter: the string to be displayed. It is also the third parameter of the string ID 3 in R. string: the length of time displayed. Toast has two default values: LENGTH_LONG (long) and LENGTH_SHORT (short). It can also be used in milliseconds, for example, 2000ms4 toast = Toast. makeText (getApplicationContext (), "simple Toast", Toast. LENGTH_LONG ). show (); of course, you can also set the image and display location: Copy code 1 toast = Toast. makeText (getApplicationContext (), "Optional Toast with image", 2 Toast. LENGTH_LONG); 3 // The first parameter: Set the position of toast displayed on the screen. My current setting is center to top 4 // The second parameter: relative to the first parameter, set the offset of the horizontal X axis at the toast position. The positive value is offset to the right, negative offset to left 5 // third parameter: Same as the second parameter 6 // If the offset you set exceeds the screen range, toast will display 7 toast near the exceeded boundary in the screen. setGravity (Gravity. CENTER,-50,100); 8 // get the toast layout 9 LinearLayout toastView = (LinearLayout) toast. getView (); 10 // set image 11 ImageView imageCodeProject = new ImageView (getApplicationContext (); 12 imageCodeProject. setImageResource (R. drawable. ic_launcher); 13 // Add image 1 4 toastView. addView (imageCodeProject, 0); 15 toast. show (); copy code or custom Toast display: Copy code 1 // LayoutInflater this class is used to instantiate the XML file to its corresponding view object layout 2 LayoutInflater inflater = getLayoutInflater (); 3 View layout = inflater. inflate (R. layout. custom, 4 (ViewGroup) findViewById (R. id. llToast); 5 ImageView image = (ImageView) layout 6. findViewById (R. id. tvImageToast); 7 image. setImageResource (R. drawable. ic_launcher); 8 TextVi Ew title = (TextView) layout. findViewById (R. id. tvTitleToast); 9 // set the title 10 title. setText ("Attention"); 11 TextView text = (TextView) layout. findViewById (R. id. tvTextToast); 12 // set content 13 text. setText ("Fully customized Toast"); 14 toast = new Toast (getApplicationContext (); 15 toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40); 16 toast. setDuration (Toast. LENGTH_LONG); 17 toast. setView (layout); 18 toast. show (); copy the code Toas T prompt message can also be from other threads: Copy code 1 Handler handler = new Handler (); 2 public void showToast () {3 handler. post (new Runnable () {4 5 @ Override 6 public void run () {7 Toast. makeText (getApplicationContext (), "I'm from another thread! ", 8 Toast. LENGTH_SHORT). show (); 9 10} 11}); 12} copy code 2. The Notification is in the mobile phone status bar. The status bar is located at the top of the mobile phone screen. It usually displays battery, signal strength, and other information. Press and hold the status bar to open the system prompt. To add a Notification, You Need To Know That icationicationmanager and Notification a and icationicationmanager notify users of the event. icationicationmanager has three common methods: 1. cancel (int id) cancels a previously displayed notification. for example, a short notification tries to hide it. For example, a persistent notification will be removed from the status bar. 2. cancelAll () cancels all previously displayed notifications. 3. Y (int id, Notification) permanently sends the notification to the status bar. b. Notification represents the attribute of a Notification: audioStreamType when the sound rings, the type of the audio stream used contentIntent. When the Notification entry is clicked, execute this Intent. contentView: When a notification is displayed on the status bar, the configured view is also displayed. defaults specifies the value to be set to the default value. deleteIntent when you click the "Clear All Notifications" button to delete All Notifications, the configured Intent is executed. the image used by the icon status bar. iconLevel if there are several levels of Status Bar images, set it here. ledARGB LED light color. flash time when ledOffMS LED is disabled (in milliseconds) Flash time when ledOnMS LED starts (in milliseconds) number indicates the voice of the event number sound notification. When the tickerText notification is displayed in the status bar, the information is displayed in vibrate vibration mode. the timestamp of the when notification. complete Notification setting code: Copy code 1 Intent I = new I Ntent (MainActivity. this, NotifiedActivity. class); 2 // pendingIntent is a special Intent. The main difference is that Intent execution is immediate, while pendingIntent execution is not immediate 3 PendingIntent pi = PendingIntent. getActivity (MainActivity. this, 0, I, 0); 4 // create a Notification object 5 Notification myNotification = new Notification (); 6 // Notification icon 7 myNotification. icon = R. drawable. header; 8 // display content of Notification 9 myNotification. tickerText = "click to view"; 10 // default sound 11 myNotification. defaults = Notification. DEFAULT_SOUND; 12 // set the notification display parameter 13 myNotification. setLatestEventInfo (MainActivity. this, "example", "click to view", pi); 14 // notification manager 15 icationicationmanager icationmanager = (icationicationmanager) getSystemService (icationication_service); 16 // send ication17 17 icationmanager. notify (0, myNotification );

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.