Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

第88章:系統服務之 NOTIFICATION_SERVICE——發送狀態欄通知(從零開始學 Android)

編輯: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 返回應用程式,系統必須能選對動作來執行,因此這個參數是不可或缺的。

一、設計界面

1. 佈局檔

打開 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。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved