Notification 的使用需要匯入 3 個類
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;
程式碼範例及說明
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent i = new Intent(arg0.getContext(), NotificationShow. class );
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!" ,
"Hello,there,I'm john." ,
contentIntent);
nm.notify(R.string.app_name, n);
下面依次對每一段代碼進行分析:
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
建立 NotificationManager,其中建立的 nm 對象負責“發出”與“取消” Notification。
Notification n = new Notification(R.drawable.chat, "Hello,there!" , System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT;
建立 Notification ,參數依次為:icon的資源id,在狀態列上展示的滾動資訊,時間。其中建立的 n 對象用來描述出現在系統通知欄的資訊,之後我們將會看到會在 n 對象上設定點擊此條通知發出的Intent。
n.flags = Notification.FLAG_AUTO_CANCEL;
設定 n.flags 為 Notification.FLAG_AUTO_CANCEL ,該標誌表示當使用者點擊 Clear 之後,能夠清除該通知。
Intent i = new Intent(arg0.getContext(), NotificationShow. class );
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
建立一個Intent,該Intent使得當使用者點擊該通知後發出這個Intent
請注意,如果要以該Intent啟動一個Activity,一定要設定 Intent.FLAG_ACTIVITY_NEW_TASK 標記。
Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在當前Task中,有要啟動的Activity,那麼把該Acitivity之前的所有Activity都關掉,並把此Activity置前以避免建立Activity的執行個體
Intent.FLAG_ACTIVITY_NEW_TASK :系統會檢查當前所有已建立的Task中是否有該要啟動的Activity的Task,若有,則在該Task上建立Activity,若沒有則建立具有該Activity屬性的Task,並在該建立的Task上建立Activity。更多請參見 “ (轉載)Android下Affinities和Task ”
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent 為Intent的封裝,這裡是啟動Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent執行個體中的Intent是用於啟動 Activity 的Intent。PendingIntent.getActivity的參數依次為:Context,寄件者的請求碼(可以填0),用於系統發送的Intent,標誌位。
其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果該描述的PendingIntent已存在,則改變已存在的PendingIntent的Extra資料為新的PendingIntent的Extra資料。
這裡再簡要說一下 Intent 與 PendingIntent 的區別:
Intent :意圖,即告訴系統我要幹什麼,然後系統根據這個Intent做對應的事。如startActivity相當於發送訊息,而Intent是訊息的內容。
PendingIntent :封裝Intent,Intent 是我們直接使用 startActivity , startService 或 sendBroadcast 啟動某項工作的意圖。而某些時候,我們並不能直接調用startActivity , startServide 或 sendBroadcast ,而是當程式或系統達到某一條件才發送Intent。如這裡的Notification,當使用者點擊Notification之後,由系統發出一條Activity 的 Intent 。因此如果我們不用某種方法來告訴系統的話,系統是不知道是使用 startActivity ,startService 還是 sendBroadcast 來啟動Intent 的(當然還有其他的“描述”),因此這裡便需要PendingIntent。
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!" ,
"Hello,there,I'm john." ,
contentIntent);
設定顯示在通知下拉框中的資訊,參數依次為:Context,標題,內容,PendingIntent。
nm.notify(R.string.app_name, n);
啟動Notification,參數依次為:在你的程式中標識Notification的id值(用來區分同一程式中的不同Notifycation,如果程式中只有一個Notification那麼這裡隨便你填什麼都可以,不過類型必須要為int),要通知的Notification。
如何使自己的Notification像Android QQ一樣能出現在 “正在啟動並執行”欄目下面
其實很簡單,只需設定Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改變 Notification 在“正在啟動並執行”欄目下面的布局
建立 RemoteViews 並賦給 Notification.contentView ,再把 PendingIntent 賦給 Notification.contentIntent 便可以了,如:
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews rv = new RemoteViews(Main. this .getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john." );
n.contentView = rv;
n.contentIntent = contentIntent;
nm.notify(R.string.app_name, n);
注意,如果使用了contentView,那麼便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在賦給 Notification.contentView 的代碼之後,那麼contentView的效果將被覆蓋,顯示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代碼之前,那麼顯示的便是 Notification.contentView 的效果,也就是說不管你想要setLatestEventInfo 或 contentView 的自訂效果,請保證始終只有一句設定代碼,因為在最後一句綁定的時候,之前的設定contentView或setLatestEventInfo的代碼都是完全沒有必要的。
shougao result(source code):
public class NotifytestActivity extends Activity {
private NotificationManager myNotificationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(this, Notifynew.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
Notification myNotification = new Notification();
myNotification.icon = R.drawable.ic_launcher;
myNotification.tickerText = "tickerText";
myNotification.defaults = Notification.DEFAULT_SOUND;
myNotification.setLatestEventInfo(this, "contenttitle", "contenttext", appIntent);
myNotificationManager.notify(0, myNotification);
}
public class Notifynew extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Toast.makeText(this, "new info", Toast.LENGTH_SHORT).show();
// finish();
}
摘自 eric_zhang的部落格