Android-自訂Notification
2014年4月26日 訊息列的訊息,想必各位Android發燒友很清楚知道是什麼,比如我們下載了一個應用,它可能會定時推送些訊息到我們的手機中,比如訊息送達的時候,可能會在通知欄顯示。本博文介紹如何自訂一個Notification,很簡單的東西,這裡小巫只是把它整理出來,奉上demo。
先來看看:vcnP1LTC66O6aHR0cDovL2Rvd25sb2FkLmNzZG4ubmV0L2RldGFpbC93d2pfNzQ4LzcyNTk4MTXT0NDLyKS1xMXz09G/ydLUvNOxvsjLtLS9qLXEyLqjrMDvw+bT0LfhuLu1xNGnz7DXytS0xbajujxzdHJvbmc+Mjk5NDAyMTMzo6jSxravv6q3or/xyMjV38i6o6k8L3N0cm9uZz48YnIgLz48YnIgLz4Jyc/D5r7NysfNqNaqwLi1xNCnufvBy6OsztLDx9b30qq4xLXEtdi3vdPQtPPNt8/xo6zQoc23z/GjrLHqzOKjrMTayN21yKOs1rG907+0tPrC66O6CTxwcmUgY2xhc3M9"brush:java;">package com.wwj.custom.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * 自訂Notification * * @author wwj * */public class MainActivity extends Activity implements OnClickListener {private Button showNotification;private Button showCustomNotifi;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showNotification = (Button) findViewById(R.id.button1);showCustomNotifi = (Button) findViewById(R.id.button2);showNotification.setOnClickListener(this);showCustomNotifi.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:send();break;case R.id.button2:custom();break;default:break;}}/** * 舊方法 */public void send() {// 1 得到通知管理器NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 2構建通知Notification notification = new Notification(android.R.drawable.stat_notify_chat, "這是提示資訊",System.currentTimeMillis());// 3設定通知的點擊事件Intent intent = new Intent(this, MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 100,intent, 0);notification.setLatestEventInfo(this, "通知的標題", "通知的內容", contentIntent);notification.flags = Notification.FLAG_AUTO_CANCEL;// 點擊通知之後自動消失// 4發送通知nm.notify(100, notification);}/** * 自訂Notification 新方法 * 新的方法,本人在手機測試會崩潰,如果不行的話,可以繼續使用舊的構建方法,畢竟高版本會相容低版本的 */public void custom() {// 1 得到通知管理器NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 2 設定通知的點擊事件Intent intent = new Intent(this, MainActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 100,intent, 0);// 3構建通知Notification.Builder builder = new Notification.Builder(this)// API 11添加的方法.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)// 設定狀態列的小標題.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.jay))// 設定下拉式清單裡的表徵圖.setWhen(System.currentTimeMillis()).setTicker("鳳姐來啦")// 設定狀態列的顯示的資訊.setAutoCancel(true)// 設定可以清除.setContentTitle("通知通知") // 設定下拉式清單裡的標題.setContentText("鳳姐即將光臨天拓遊戲,各部門做好防雷準備"); // 設定可以清除Notification notification = builder.build();// API 16添加建立notification的方法// 通知manager.notify(110, notification);// // 2構建通知// Notification notification2 = new Notification(R.drawable.jay, "天拓遊戲",// System.currentTimeMillis());//// // 3設定通知的點擊事件// Intent intent2 = new Intent(this, MainActivity.class);// PendingIntent contentIntent2 = PendingIntent.getActivity(this, 100,// intent2, 0);// notification2.setLatestEventInfo(this, "天拓遊戲", "天拓遊戲有個技術部",// contentIntent2);//// notification2.flags = Notification.FLAG_AUTO_CANCEL;// 點擊通知之後自動消失//// // 4發送通知// manager.notify(100, notification2);}}
稍微提一下的是,我們都知道Android SDK版本的變遷,API也會跟著遍,每個版本的API都可能會增刪改一些介面,我們在使用Android為我們開發人員提供的一些方法的時候,需要注意版本之間的區別,假如我們使用高版本的API的話,可能會引起一些錯誤,低版本的話可能在開發中又不適用了,反正程式不崩潰是最起碼的保證,至於程式的功能能實現用什麼API都是可以的。