最近發現有時間就一直在上網或看書學,但是都沒自己寫個什麼東西實在很容易忘記

而且似乎感覺有越來越難的趨勢~ @@,並且記了後面忘了前面~~

只好先寫下來供以後回想使用....

這次學的是 Notification 的用法,就是上方可下拉的那個訊息通知列

 

首先要先

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

 

NotificationManager barManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification barMsg = new Notification(R.drawable.icon,"您有新通知",System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,Report.class), PendingIntent.FLAG_UPDATE_CURRENT);

barMsg.setLatestEventInfo(this, "我是通知訊息", "我是通知內容", contentIntent);

barManager.notify(0,barMsg);

=============================================================

   先定義一個管理訊息的物件 barManager,getSystemService 傳入相關的常數就可以取得系統提供的功能

   請見 :  http://developer.android.com/reference/android/content/Context.html

  

   再來建立一個放置內容訊息的 Notification 物件  barMsg

   用法 : 

   public Notification (int icon, CharSequence tickerText, long when)

   請見 : http://developer.android.com/reference/android/app/Notification.html#Notification()


   之後建立一個 PendingIntent 來包裝 Intent

   但我實在還很不清楚  new Intent(this,Report.class)  是何意思

   用法有點給他複雜

   http://developer.android.com/reference/android/content/Intent.html

   用法 : 

   public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)


   再來用 setLatestEventInfo 描述一下通知內容要什麼訊息

   用法 :

   public void setLatestEventInfo (Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)


   最後用 NotificationManager 的 notify 方法將 barMsg 傳送給 android 框架 

   用法 :

   public void notify (int id, Notification notification)

 

 

 

  結束!

arrow
arrow
    全站熱搜

    小雕 發表在 痞客邦 留言(1) 人氣()