編輯:Android 技術基礎
所謂 Notification(通知),就是在系統狀態欄(一般來說 Android 手機的狀態欄在頂部,而非底部,這點要特別留意)放上一個圖示,並附上一條可選的提示文字,同時在通知視窗裡加入一筆展開資訊。當使用者點選這筆展開資訊時,Android 便會執行該通知預先定義好的 Intent(通常用來彈出一個 Activity)。你還可以為通知搭配設備的聲音、震動、閃光來提醒使用者。
那麼,什麼時候會用到狀態欄通知呢?當後台服務(Service)需要對某個事件發出提醒、並且希望使用者有所回應時,它就派上用場了。後台服務從不該自己跳出 Activity 來索取互動,正確做法是先發出一則狀態欄通知,等使用者點選後,再由通知去啟動 Activity。
Activity 與 Service 都能初始化狀態欄通知。不過由於 Activity 只有在活躍且取得焦點時才能操作,實務上多數會選擇用 Service 來創建通知——這樣即使使用者正在用別的程式,甚至設備已休眠,通知依然能從後台發出。要創建通知,得動用兩個類:Notification 與 NotificationManager。
NotificationManager 是一項 Android 系統服務,負責管理與運行所有通知。它不能被直接實例化,而是要靠 getSystemService() 取得引用;到了需要提醒使用者時,再呼叫 notify() 把 Notification 物件交給它。
(1)取得 NotificationManager 的引用
NotificationManager notificationManager=(NotificationManager) super.getSystemService(Context.NOTIFICATION_SERVICE);
(2)實例化 Notification
Notification notification=new Notification( R.drawable.ic_launcher, "Notification消息提示!", System.currentTimeMillis());
(3)指定通知的展開資訊與 Intent
PendingIntent intent=PendingIntent.getActivity( this, 0, super.getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo( this, "跟我學Android", "跟我學編程:www.genwoxue.com", intent);
(4)把 Notification 物件交給 NotificationManager
notificationManager.notify( "Genwoxue", R.drawable.ic_launcher, notification);
要發送狀態欄通知,只要透過 notify(int, Notification) 把 Notification 物件傳給 NotificationManager 即可。第一個參數是通知的唯一 ID,第二個參數是 Notification 物件。ID 在整個應用程式範圍內唯一標識一則通知——當 App 管理著多種不同通知時,使用者透過各自定義的 Intent 返回應用程式,系統必須能選對動作來執行,因此這個參數是不可或缺的。
打開 res/layout/activity_main.xml,這裡只放一段說明文字即可:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/notifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification通知將一個圖標填加到系統的狀態欄中,並將一條展開信息添加到通知窗口中。" />
</LinearLayout>
打開 src/com.genwoxue.notification/MainActivity.java:
package com.genwoxue.notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationManager notificationManager=(NotificationManager)super.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification=new Notification(
R.drawable.ic_launcher,
"Notification消息提示!",
System.currentTimeMillis());
PendingIntent intent=PendingIntent.getActivity(
this,
0,
super.getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(
this,
"跟我學Android",
"跟我學編程:www.genwoxue.com",
intent);
notificationManager.notify(
"Genwoxue",
R.drawable.ic_launcher,
notification);
}
}
沒有特殊權限需求,採用預設的 AndroidManifest.xml 即可。
App 一啟動,狀態欄便會出現通知圖示;下拉通知視窗可看見展開的標題與內容,點擊後即會跳到對應的 Activity。



Android 觸摸事件:OnTouchListener、OnTouchEvent 與多點觸碰
OnTouchListener 與 OnTouchEvent 的差異 Android 的觸摸處理有兩種常見方式:OnTouchListener 是監聽模式,通常附加在 V
第26章 Android OnKeyListener 鍵盤事件:銀行卡號四位一組即時回顯
鍵盤事件最常被拿來做兩件事:即時驗證輸入格式(例如 email 是否合法),以及過濾不該出現的字元。本章做一個更貼近生活的例子——輸入銀行帳號,每四位自動用「-」分隔,並
Paint API 之 Xfermode 與 PorterDuff 詳解(四):DST_OUT 手撕圖片效果
本節引言: 上節我們寫了 Xfermode 與 PorterDuff 使用的第一個例子:圓角與圓形圖片 ImageView 的實現,體會到了 PorterDuff.Mod
2.5.6 ViewFlipper 翻轉視圖實作輪播
前言 本節介紹 ViewFlipper,它是 Android 自帶的多頁面管理元件,而且能自動播放。和 ViewPager 不同,ViewPager 是「一頁頁」地換,