- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境搭建
- Android - 架構
- Android - 應用元件
- Android - Hello World 示例
- Android - 資源
- Android - 活動(Activity)
- Android - 服務(Service)
- Android - 廣播接收器(Broadcast Receiver)
- Android - 內容提供器(Content Provider)
- Android - 碎片(Fragment)
- Android - 意圖/過濾器(Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 樣式和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 電話呼叫
- 釋出 Android 應用
- Android 實用示例
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊捕捉
- Android - 音訊管理器
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - Google 地圖
- Android - 圖片特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入旋轉器
- Android - 本地化
- Android - 登入螢幕
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - 推送通知
通知是在應用程式正常 UI 之外顯示給使用者的訊息。您可以在 Android 中非常輕鬆地建立自己的通知。
Android 為此目的提供了NotificationManager 類。為了使用此類,您需要透過getSystemService() 方法請求 Android 系統來例項化此類的物件。其語法如下所示:
NotificationManager NM; NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
之後,您將透過Notification 類建立通知並指定其屬性,例如圖示、標題和時間等。其語法如下所示:
Notification notify = new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());
接下來需要做的是透過傳遞上下文和意圖作為引數來建立PendingIntent。透過向另一個應用程式提供 PendingIntent,您授予它執行您指定的 operatio 的許可權,就好像另一個應用程式是自己一樣。
PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(),0);
最後需要做的是呼叫 Notification 類的setLatestEventInfo 方法,並將 pending intent 與通知主題和正文詳細資訊一起傳遞。其語法如下所示。然後最後呼叫 NotificationManager 類的 notify 方法。
notify.setLatestEventInfo(getApplicationContext(), subject, body,pending); NM.notify(0, notify);
除了 notify 方法之外,NotificationManager 類中還提供了其他方法。它們列在下面:
| 序號 | 方法和描述 |
|---|---|
| 1 |
cancel(int id) 此方法取消先前顯示的通知。 |
| 2 |
cancel(String tag, int id) 此方法也取消先前顯示的通知。 |
| 3 |
cancelAll() 此方法取消所有先前顯示的通知。 |
| 4 |
notify(int id, Notification notification) 此方法釋出要在狀態列中顯示的通知。 |
| 5 |
notify(String tag, int id, Notification notification) 此方法還在狀態列中釋出要顯示的通知。 |
示例
以下示例演示了 NotificationManager 類的用法。它建立了一個基本的應用程式,允許您建立通知。
要試驗此示例,您需要在實際裝置或模擬器上執行它。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio 在包 com.example.sairamkrishna.myapplication 下建立一個 Android 應用程式。 |
| 2 | 修改 src/MainActivity.java 檔案以新增通知程式碼。 |
| 3 | 修改佈局 XML 檔案 res/layout/activity_main.xml,如果需要新增任何 GUI 元件。 |
| 4 | 執行應用程式並選擇一個正在執行的 Android 裝置,並在其上安裝應用程式並驗證結果。 |
以下是MainActivity.java的內容。
在以下程式碼中,abc表示 tutorialspoint.com 的徽標
package com.example.sairamkrishna.myapplication;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
EditText ed1,ed2,ed3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
ed3=(EditText)findViewById(R.id.editText3);
Button b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tittle=ed1.getText().toString().trim();
String subject=ed2.getText().toString().trim();
String body=ed3.getText().toString().trim();
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle(tittle).setContentText(body).
setContentTitle(subject).setSmallIcon(R.drawable.abc).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
});
}
}
以下是activity_main.xml的內容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="52dp"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:hint="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Subject"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText3"
android:hint="Body"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_alignEnd="@+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification"
android:id="@+id/button"
android:layout_marginTop="77dp"
android:layout_below="@+id/editText3"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
</RelativeLayout>
以下是AndroidManifest.xml的內容。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
讓我們嘗試執行我們的應用程式。要從 Android Studio 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的執行
圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇要在其中執行 Android 應用程式的選項。
現在填寫標題、主題和正文欄位。這已在下圖中顯示:
現在單擊通知按鈕,您將在頂部通知欄中看到一個通知。這已在下圖中顯示:
現在向下滾動通知欄並檢視通知。這已在下圖中顯示:
