Android添加頂部通知(Notification)並處於“進行中中”(onGoing)

來源:互聯網
上載者:User

用過Android版的QQ的都知道,QQ返回的時候在頂部通知裡會出現一個企鵝,表明QQ正在運行,可以拉開通知,點擊手機QQ出現主介面,繼續運行。

所以我想讓自己的軟體也出現這麼一個通知,並且可以快速的開啟查看。

Android應用開發詳解 8.3 Notification和NotificationManager的使用 P178

 /**
* 添加頂部通知
* @author liuzhao
*/
public void AddNotification(){
//添加通知到頂部工作列
//獲得NotificationManager執行個體
String service = NOTIFICATION_SERVICE;
nm = (NotificationManager)getSystemService(service);
//執行個體化Notification
n = new Notification();
//設定顯示表徵圖
int icon = R.drawable.ic_launcher_home;
//設定提示資訊
String tickerText = “我的程式”;
//顯示時間
long when = System.currentTimeMillis();

n.icon = icon;
n.tickerText = tickerText;
n.when = when;
//顯示在“進行中中”
n.flags = Notification.FLAG_ONGOING_EVENT;

//執行個體化Intent
Intent intent = new Intent(tykmAndroid.this,tykmAndroid.class);
//獲得PendingIntent
PendingIntent pi = PendingIntent.getActivity(tykmAndroid.this, 0, intent, 0);
//設定事件資訊,顯示在拉開的裡面
n.setLatestEventInfo(tykmAndroid.this, “我的軟體”, “我的軟體正在運行……”, pi);
//發出通知
nm.notify(ID,n);
}

 

-------------------------------------------------------------------------------------

 

進行中的和持續的Notification

 

通過設定FLAG_INSISTENT和FLAG_ONGOING_EVENT 標誌位可以讓Notification成為持續或進行中的Notification。

 

Notification標記為ONGOING,如下面的代碼所示,它就能用於表示當前進行中的事件(如來電)。進行中的事件與“普通的”Notification區別在擴充的狀態條視窗中。

 

notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

 

持續的Notification一直重複,直到使用者取消。下面的代碼給出了如何設定Notification為持續的:

 

notification.flags = notification.flags | Notification.FLAG_INSISTENT;

 

持續Notification反覆重複開頭的Notification效果,直到使用者取消。持續的Notification應該保留給如鬧鐘的情形,它需要及時的採取響應。

 

通過設定FLAG_INSISTENT和FLAG_ONGOING_EVENT 標誌位可以讓Notification成為持續或進行中的Notification。

 

Notification標記為ONGOING,如下面的代碼所示,它就能用於表示當前進行中的事件(如來電)。進行中的事件與“普通的”Notification區別在擴充的狀態條視窗中。

 

notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

 

持續的Notification一直重複,直到使用者取消。下面的代碼給出了如何設定Notification為持續的:

 

notification.flags = notification.flags | Notification.FLAG_INSISTENT;

 

持續Notification反覆重複開頭的Notification效果,直到使用者取消。持續的Notification應該保留給如鬧鐘的情形,它需要及時的採取響應。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.