Android's "Notification" methods (Notification & amp; NotificationManager) and androidnotification

Source: Internet
Author: User

Notification & NotificationManager and androidnotification

I believe everyone is familiar with Notification. When a message arrives or receives a text message on QQ, new messages or something will be displayed on the top of the mobile phone, just like this. Today, we will record the usage of several types of notifications. There are some differences between notifications before 3.0 and those after 3.0. If you don't have much to say, go directly to the code.

1. Create an android Project

MinSdkVersion = "11", targetSdkVersion = "19" of my new project ". That is, the minimum version of 3.0 is supported.

2. habitually open the project list file AndroidManifest. xml and add a permission: <uses-permission android: name = "android. permission. VIBRATE"/>.

3. Add several buttons in activity_main.xml.

Code

 

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" android: orientation = "vertical" tools: context = ". mainActivity "> <Button android: id =" @ + id/btn_01 "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: text = "notification of earlier versions 3.0, use the new" android: onClick = "click"/> <Button android: id = "@ + id/btn_02" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "large view text notification" android: onClick = "click"/> <Button android: id = "@ + id/btn_03" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "large view image notification" android: onClick = "click"/> <Button android: id = "@ + id/btn_04" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "progress bar notification" android: onClick = "click"/> </LinearLayout>View Code

 

 

4. MainActivity code:

1 package com. xin. day_12_notificationdemo1; 2 3 import java. util. timer; 4 import java. util. timerTask; 5 6 import android. app. activity; 7 import android. app. notification; 8 import android. app. icationicationmanager; 9 import android. app. pendingIntent; 10 import android. content. intent; 11 import android. graphics. bitmapFactory; 12 import android. OS. bundle; 13 import android. support. v4.app. notificationCom Pat; 14 import android. support. v4.app. icationicationcompat. bigPictureStyle; 15 import android. support. v4.app. icationicationcompat. bigTextStyle; 16 import android. support. v4.app. icationicationcompat. builder; 17 import android. util. log; 18 import android. view. view; 19 20 public class MainActivity extends Activity {21 22 // the unique identifier of the notification, in an application, different notifications must be differentiated. 23 private static final int NO1 = 0x1001; 24 private sta Tic final int NO2 = 0x1002; 25 private static final int NO3 = 0x1003; 26 private static final int NO4 = 0x1004; 27 // The progress bar should be 28 private int progress = 1; 29 30 @ Override 31 protected void onCreate (Bundle savedInstanceState) {32 super. onCreate (savedInstanceState); 33 setContentView (R. layout. activity_main); 34} 35 36 // click method, which must be consistent with the onClick attribute values of buttons in the xml file. 37 public void click (View view) {38 // create Noti FicationManager 39 final NotificationManager manager = (icationicationmanager) getSystemService (icationication_service); 40 // use the switch statement to control the four controls 41 switch (view. getId () {42 case R. id. btn_01: {43 Notification notification = new Notification (); 44 notification. icon = R. drawable. ic_launcher; 45 notification. tickerText = "there is a message... "; 46 Intent intent = new Intent (this, MainActivity. class); 47 PendingIntent pendingIntent = PendingIntent. getActivity (this, 0, 48 intent, PendingIntent. FLAG_UPDATE_CURRENT); 49 notification. setLatestEventInfo (this, "notifications before 3.0", "try", pendingIntent); 50 notification. when = System. currentTimeMillis (); 51 notification. defaults = Notification. DEFAULT_ALL; 52 notification. flags = Notification. FLAG_AUT O_CANCEL; 53 notification. number = 1; 54 notification. vibrate = new long [] {0, 4000}; 55 manager. notify (NO1, notification); 56} 57 break; 58 case R. id. btn_02: {59 // large view text notification 60 // create a message constructor in the extended package 61 icationicationcompat. builder builder = new NotificationCompat. builder (this); 62 // set when a message is prompted, icon and prompt text 63 builder. setSmallIcon (R. drawable. ic_launcher ). setTicker ("new message"); 64 // style 65 BigTextStyle = new B IgTextStyle (); 66 style. setBigContentTitle ("class notification"); // notification title 67 style. bigText ("jsp on B303 this afternoon"); // notification text content 68 // large view text content 69 style. setSummaryText ("this is a normal course schedule, please attend classes on time"); 70 builder. setStyle (style); 71 // display the message arrival time. Set the current time 72 builder here. setWhen (System. currentTimeMillis (); 73 // get a Notification object 74 notification Notification = builder. build (); 75 notification. flags = Notification. FLAG_AUTO_CANCEL; 76 // send (Display) Notification 7 7 // Policy () the first parameter id An identifier for this notification unique within your application 78 // get? This notification uniquely identifies 79 manager in your application. listen y (NO2, notification); 80} 81 break; 82 83 case R. id. btn_03: {84 // large view image notification 85 icationicationcompat. builder builderPic = new Builder (this); 86 builderPic. setSmallIcon (R. drawable. ic_launcher ). setTicker ("Sina sports reminder"); 87 // set 88 BigPictureStyle pictureStyle = new BigPictureStyle (); 89 pictureStyle. setBigContentTitle ("Sina sports clippers VS server guard"); 90 pictureStyle. bigPicture (BitmapF Acloud. decodeResource (getResources (), R. drawable. ic_game); 91 pictureStyle. setSummaryText (" VS battle !!! "); // Do not care about the text 92 // set the style 93 builderPic. setStyle (pictureStyle); 94 // set the display time to 95 builderPic. setWhen (System. currentTimeMillis (); 96 Notification notification = pictureStyle. build (); 97 notification. flags = Notification. FLAG_AUTO_CANCEL; 98 // 99 manager. notify (NO3, notification); 100} 101 break; 102 103 case R. id. btn_04: {104 // progress bar notification 105 final NotificationCompat. builder builderProgress = new NotificationCompat. builder (this); 106 builderProgress. setSmallIcon (R. drawable. ic_launcher ). setTicker ("progress bar notification"); 107 builderProgress. setProgress (100, progress, false); 108 final Notification notification = builderProgress. build (); 109 // send a notification 110 manager. Y (NO4, notification); 111 // create a Timer 112 timer Timer = new timer (); 113 Timer. schedule (new TimerTask () {114 115 @ Override116 public void run () {117 Log. I ("progress", progress + ""); 118 while (progress <= 100) {119 progress ++; 120 try {121 Thread. sleep (300); 122} catch (InterruptedException e) {123 // TODO Auto-generated catch block124 e. printStackTrace (); 125} 126 // update progress bar 127 builderProgress. setProgress (100, progress, false); 128 // notify the 129 manager again. Y (NO4, builderProgress. build (); 130} 131 // The timer exits 132 this. cancel (); 133 // progress bar to exit the 134 manager. cancel (NO4); 135 return; // end method 136} 137 138}, 0); 139} 140 break; 141 142 143 default: 144 break; 145} 146} 147}View Code

 

5. Run: My virtual machine version is 4.0 (api19). Press and hold the notification and Slide left (right) to set the notification duration. The effect is as follows:

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.