關於通知Notification相信大家都不陌生了,平時上QQ的時候有消息來了或者有收到了短信,手機頂部就會顯示有新消息什么的,就類似這種。今天就稍微記錄下幾種Notification的用法。3.0以前的通知和3.0以后的通知是有些區別的。話不多說,直接上代碼。
1、新建一個android項目
我新建項目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的。
2、習慣性地打開項目清單文件AndroidManifest.xml,添加一個權限:<uses-permission android:name="android.permission.VIBRATE"/> 不添加不行的。
3、在布局activity_main.xml中添加幾個按鈕,樣子就大概這樣,垂直排版的LinearLayout
具體代碼

<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/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3.0以前版本的notification,用新的吧" android:onClick="click" /> <Button android:id="@+id/btn_02" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="大視圖文本通知" android:onClick="click" /> <Button android:id="@+id/btn_03" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="大視圖圖片通知" android:onClick="click" /> <Button android:id="@+id/btn_04" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="進度條通知" android:onClick="click" /> </LinearLayout>
4、MainActivity中的代碼:

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.NotificationManager; 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.NotificationCompat; 14 import android.support.v4.app.NotificationCompat.BigPictureStyle; 15 import android.support.v4.app.NotificationCompat.BigTextStyle; 16 import android.support.v4.app.NotificationCompat.Builder; 17 import android.util.Log; 18 import android.view.View; 19 20 public class MainActivity extends Activity { 21 22 //通知的唯一標識,在一個應用程序中不同的通知要區別開來 23 private static final int NO1 = 0x1001; 24 private static final int NO2 = 0x1002; 25 private static final int NO3 = 0x1003; 26 private static final int NO4 = 0x1004; 27 //進度條要用 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方法,和xml文件中的各個按鈕的onClick屬性的值要一致 37 public void click(View view) { 38 //創建NotificationManager 39 final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 40 //用switch語句控制四個控件 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 = "有消息了。。。"; 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, "3.0以前的通知", "試試而已", pendingIntent); 50 notification.when = System.currentTimeMillis(); 51 notification.defaults = Notification.DEFAULT_ALL; 52 notification.flags = Notification.FLAG_AUTO_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 //大視圖文本通知 60 //創建消息構造器,在擴展包 61 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 62 //設置當有消息是的提示,圖標和提示文字 63 builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了"); 64 //需要樣式 65 BigTextStyle style = new BigTextStyle(); 66 style.setBigContentTitle("上課通知");//通知的標題 67 style.bigText("今天下午要在綜B303上jsp");//通知的文本內容 68 //大視圖文本具體內容 69 style.setSummaryText("這是正常的課程安排,請各位同學按時上課"); 70 builder.setStyle(style); 71 //顯示消息到達的時間,這里設置當前時間 72 builder.setWhen(System.currentTimeMillis()); 73 //獲取一個通知對象 74 Notification notification = builder.build(); 75 notification.flags = Notification.FLAG_AUTO_CANCEL; 76 //發送(顯示)通知 77 //notify()第一個參數id An identifier for this notification unique within your application 78 //get?意思說,這個通知在你的應用程序中唯一的標識符 79 manager.notify(NO2, notification); 80 } 81 break; 82 83 case R.id.btn_03:{ 84 //大視圖圖片通知 85 NotificationCompat.Builder builderPic = new Builder(this); 86 builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪體育提醒"); 87 //進行設置 88 BigPictureStyle pictureStyle = new BigPictureStyle(); 89 pictureStyle.setBigContentTitle("新浪體育 快船VS騎士 "); 90 pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game)); 91 pictureStyle.setSummaryText(" 快船VS騎士 天王山之戰!!!");//不要在意文字 92 //設置樣式 93 builderPic.setStyle(pictureStyle); 94 //設置顯示的時間 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 //進度條通知 105 final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this); 106 builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("進度條通知"); 107 builderProgress.setProgress(100, progress, false); 108 final Notification notification = builderProgress.build(); 109 //發送一個通知 110 manager.notify(NO4, notification); 111 //創建一個計時器 112 Timer timer = new Timer(); 113 timer.schedule(new TimerTask(){ 114 115 @Override 116 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 block 124 e.printStackTrace(); 125 } 126 //更新進度條 127 builderProgress.setProgress(100, progress, false); 128 //再次通知 129 manager.notify(NO4, builderProgress.build()); 130 } 131 //計時器退出 132 this.cancel(); 133 //進度條退出 134 manager.cancel(NO4); 135 return;//結束方法 136 } 137 138 }, 0); 139 } 140 break; 141 142 default: 143 break; 144 } 145 } 146 147 }
5、運行:我的虛擬機版本是4.0的,按住通知左(右)滑動就可以讓通知小時了。效果如下: