1.獲取NotificationManager
String service = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(service);
2.實例化Notification對象
- //實例化Notification
- Notification notification = new Notification();
3.設置Notification的屬性
- // 設置顯示圖標,該圖標會在狀態欄顯示
- int icon = notification.icon = R.drawable.happy;
- // 設置顯示提示信息,該信息也在狀態欄顯示
- String tickerText = "測試Notification";
- // 顯示時間
- long when = System.currentTimeMillis(); notification.icon = icon;
- notification.tickerText = tickerText;
- notification.when = when;
- //也可以這樣設置
- Notification notification_2=new Notification(icon,tickerText,when)
調用setLatestEventInfo()方法在視圖中設置圖標和時間。
- // 實例化Intent
- Intent intent = new Intent(MainActivity.this, MainActivity.class);
- // 獲得PendingIntent
- PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
- // 設置事件信息
- notification.setLatestEventInfo(MainActivity.this, " Title", "Content", pIntent);
4.發出通知
- //Notification標示ID
- private static final int ID = 1;
- //發出通知
- mNotificationManager.notify(ID, n);
下面是具體的例子,在這個例子裡定義了一個MainActivity發出廣播通知,定義一個MyReceiver類繼承Broadcasts接受通知,當接收完通知之後,啟動一個SecondActivity,在SecondActivity類中通過Notification和NotificationManager來可視化顯示廣播通知。具體的步驟如下:
MainActivity.java
- package com.Android.notification;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- // 聲明Button
- private Button btn;
- // 定義Broadcast Receiver action
- private static final String MY_ACTION = "com.android.notification.MY_ACTION";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 設置當前布局視圖
- setContentView(R.layout.main);
- // 實例化Button
- btn = (Button)findViewById(R.id.Button1);
- // 添加事件監聽器
- btn.setOnClickListener(listener);
- }
- // 創建事件監聽器
- private OnClickListener listener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 實例化Intent
- Intent intent = new Intent();
- // 設置Intent action屬性
- intent.setAction(MY_ACTION);
- // 發起廣播
- sendBroadcast(intent);
- }
- };
- }
MyReceiver.java
- package com.android.notification;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class MyReceiver extends BroadcastReceiver{
- @Override
- public void onReceive(Context context, Intent intent) {
- // 實例化Intent
- Intent i = new Intent();
- // 在新的任務中啟動Activity
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- // 設置Intent啟動的組件名稱
- i.setClass(context, SecondActivity.class);
- // 啟動Activity顯示通知
- context.startActivity(i);
- }
- }
SecondActivity.java
- package com.android.notification;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class SecondActivity extends Activity {
- // 聲明按鈕
- private Button cancelBtn;
- // 聲明Notification
- private Notification notification ;
- // 聲明NotificationManager
- private NotificationManager mNotification;
- // Notification標示ID
- private static final int ID = 1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- // 實例化按鈕
- cancelBtn = (Button)findViewById(R.id.cancelButton2);
- // 獲得NotificationManager實例
- String service = NOTIFICATION_SERVICE;
- mNotification = (NotificationManager)getSystemService(service);
- // 實例化Notification
- notification = new Notification();
- // 設置顯示圖標,該圖標會在狀態欄顯示
- int icon = notification.icon = android.R.drawable.stat_notify_chat;
- // 設置顯示提示信息,該信息也會在狀態欄顯示
- String tickerText = "Test Notification";
- // 顯示時間
- long when = System.currentTimeMillis();
- notification.icon = icon;
- notification.tickerText = tickerText;
- notification.when = when;
- // 實例化Intent
- Intent intent = new Intent(this, MainActivity.class);
- // 獲得PendingIntent
- PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
- // 設置事件信息
- notification.setLatestEventInfo(this, "消息", "Hello Android", pi);
- // 發出通知
- mNotification.notify(ID, notification);
- // 為按鈕添加監聽器
- cancelBtn.setOnClickListener(cancelListener);
- }
- // 取消通知監聽器
- private OnClickListener cancelListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 取消通知
- mNotification.cancel(ID);
- }
- };
- }