Monodroid study note (12)-You have a new short message. Check it carefully. Status Bar reminder

Source: Internet
Author: User

At the top of the Android mobile phone interface, there is an area showing time, signal strength, battery status, and other information. This is the status bar of Android. When the system has some information to notify the mobile phone user, such as receiving a new text message, email, or missed call, the system usually displays the information in the status bar, and some only show small icons, some display icons and text reminders, and press and hold the status bar with your fingers to pull down. You can also expand the status bar to view all information sent by the system.

In the program, how do I put the prompt information into the status bar and display the small icon? To manage notification, Android API defines icationmanager manager. You only need to call the y method to display the information in the status bar.

First, prepare several images for display, then place a spinner on the interface, and change the corresponding icon by clicking different options. The layout file is simple:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <spinner Android: Id = "@ + ID/myspinnner" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"> </spinner> <br/> </relativelayout> 

 

Icationicationmanager nm; <br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> try <br/> {<br/> Nm = This. getsystemservice (service. notificationservice) as icationicationmanager; <br/> spinner myspinner = findviewbyid <spinner> (resource. id. myspinnner); <br/> arrayadapter <string> adapter = new arrayadapter <string> (this, android. resource. layout. simplespinneritem, new string [] {"show icon 1", "Show Icon 2", "Show icon 3", "Show icon 4", "Close display "}); <br/> myspinner. adapter = adapter; <br/> myspinner. itemselected + = (sender, e) =>< br/>{< br/> switch (E. position) <br/>{< br/> case 0: <br/> using Y (resource. drawable. btn1, "Notification 1"); <br/> break; <br/> case 1: <br/> condition y (resource. drawable. btn2, "Notice 2"); <br/> break; <br/> case 2: <br/> condition y (resource. drawable. btn3, "Notice 3"); <br/> break; <br/> case 3: <br/> condition y (resource. drawable. btn4, "Notice 4"); <br/> break; <br/> case 4: <br/> nm. cancel (0); <br/> break; <br/>}< br/>}; <br/>}< br/> catch (system. exception ex) <br/>{< br/> MessageBox. showerrormessage (this, ex); <br/>}< br/> private void notify (INT iconid, string text) <br/>{< br/> activity2 Act2 = new activity2 (); <br/> intent policyintent = new intent (this, act2.class); <br/> policyintent. setflags (activityflags. newtask); <br/> pendingintent appintent = pendingintent. getactivity (this, 0, policyintent, 0); <br/> notification n = new notification <br/>{< br/> icon = iconid, <br/> tickertext = new Java. lang. string (text), <br/> defaults = notificationdefaults. sound, <br/>}; <br/> N. setlatesteventinfo (this, "notification", text, appintent); <br/> nm. Y (0, n); <br/>}< br/>


When you pull the status bar down and click notification, activity2 is opened and a toast is displayed in activity2.

[Activity (Label = "My activity")] <br/> public class activity2: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. activity2); <br/> toast. maketext (this, "opened by clicking in the notification area", toastlength. short ). show (); <br/>}< br/> 

 

Running effect:

 

When a notification is sent, the text is displayed in three places. The first is the text that runs out of the icon when the notification is added to the status bar, and N is used in the program. tickertext. The other two items are the information title and content displayed in the notification list. The setlatesteventinfo () method is set. If the text is not displayed in the status bar and only the image is displayed, you only need to set tickertext.

If we want to shake the phone when the notification is displayed, we need to use the vibrator object. In setting a vibration event, you must know the duration of the command's vibration and the cycle of the vibration event. The values to be set in Android are calculated in milliseconds, therefore, when setting, you must note that if the set time is too small, you will not feel it.

 

Vibrator v = This. application. getsystemservice (service. vibratorservice) as vibrator; <br/> v. vibrate (New long [] {100, 10,100,100 0}, 0 ); 

 

The first parameter of the vibrate method is a long array, indicating the vibration frequency. You can change the length and value of the array to try the effect. The second repeat parameter. When it is equal to 0, the vibration will continue. If it is equal to-1, the vibration will appear in a round, and no action will be taken after the vibration is completed.

To shake your phone, you must add the vibrate permission to androidmanifest. xml.

<Uses-Permission Android: Name = "android. Permission. Vibrate"/> 

I remember that the simulator could not simulate the vibration ~~ Copy it to your phone and shake it ~~

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.