編輯:關於Android編程
Notification可以讓我們在獲得消息的時候,在狀態欄,鎖屏界面來顯示相應的信息,很難想象如果沒有Notification,那我們的qq和微信以及其他應用沒法主動通知我們,我們就需要時時的看手機來檢查是否有新的信息和提醒著實讓人煩心,也體現出Notification重要性。這裡會介紹三種Notification,分別是普通的Notification,折疊式Notification和懸掛式Notification。
1. 普通Notification
首先創建Builder 對象,用PendingIntent 控制跳轉,這裡跳轉到網頁
Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(http://blog.csdn.net/itachi85/));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
有了builder 我們就可以給Notification添加各種屬性:
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle(普通通知);
最後是創建NotificationManager調用notify方法:
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
來看看效果:
2. 折疊式Notification<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxiciAvPg0K1du1/sq9Tm90aWZpY2F0aW9uysfSu9bW19S2qNLlytPNvLXETm90aWZpY2F0aW9uo6zTw8C0z9TKvrOkzsSxvrrN0rvQqdfUtqjS5bXEsry+1rXEs6G+sKGjy/zT0MG91tbXtMyso6zSu9bWysfG1c2o17TMrM/CtcTK0828o6jI57n7srvKx9fUtqjS5bXEu7C6zcnPw+bG1c2ozajWqrXEytPNvNH5yr3Su9H5o6mjrNK71tbKx9W5v6rXtMysz8K1xMrTzbyho7rNxtXNqE5vdGlmaWNhdGlvbrK7zay1xMrHo6zO0sPH0OjSqtfUtqjS5bXEytPNvKOstvjV4rj2ytPNvM/Uyr61xL34s8y6zc7Sw8e0tL2oytPNvLXEvfizzLK71NnSu7j2vfizzKOsy/nS1M7Sw8fQ6NKqyrnTw1JlbW90ZVZpZXdzo6zK18/I0qrKudPDUmVtb3RlVmlld3PAtLS0vajO0sPHtcTX1Lao0uXK0828OjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">
視圖的布局文件: 我們需要把自定義的視圖賦值給Notification的視圖,下面代碼是把自定義視圖賦值給Notification展開時的視圖 當然我們也可以把自定義視圖賦值給Notification普通狀態時的視圖 其他的代碼和普通Notification沒什麼區別,折疊式Notification完整代碼: 如果不是自定義普通狀態視圖的話,折疊式Notification普通狀態下和普通Notification沒什麼區別 我們接著往下拉,使折疊式Notification完全展開就會出現我們自定義的視圖 3. 懸掛式Notification 實現懸掛式Notification完整代碼: 來看看效果 4. Notification的顯示等級 VISIBILITY_SECRET 在安全鎖和沒有鎖屏的情況下顯示通知 設置非常簡單只要調用setVisibility方法就可以了 我在這裡寫了個方法來設置Notification等級,用radioGroup來演示Notification的各個顯示等級,詳情請參照源碼。 //用RemoteViews來創建自定義Notification視圖
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
//指定展開時的視圖
notification.bigContentView = remoteViews;
//指定普通狀態時的視圖
notification.contentView = remoteViews;
Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(http://blog.csdn.net/itachi85/));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle(折疊式通知);
//用RemoteViews來創建自定義Notification視圖
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
Notification notification = builder.build();
//指定展開時的視圖
notification.bigContentView = remoteViews;
notificationManager.notify(1, notification);
懸掛式Notification是android5.0新增加的方式,和前兩種顯示方式不同的是,前兩種需要下拉通知欄才能看到通知,而 懸掛式Notification不需要下拉通知欄就直接顯示出來懸掛在屏幕上方並且焦點不變仍在用戶操作的界面因此不會打斷用戶的操作,過幾秒就會自動消失。
和前兩種Notification不同的是,他需要調用setFullScreenIntent來將Notification變為懸掛式Notification
//如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);
Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(http://blog.csdn.net/itachi85/));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.foldleft);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true);
builder.setContentTitle(懸掛式通知);
//設置點擊跳轉
Intent hangIntent = new Intent();
hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
hangIntent.setClass(this, MyNotificationActivity.class);
//如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(hangPendingIntent, true);
notificationManager.notify(2, builder.build());
android5.0加入了一種新的模式Notification的顯示等級,共有三種:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
private void selectNotofovatiomLevel(Notification.Builder builder) {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rb_public:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText(public);
break;
case R.id.rb_private:
builder.setVisibility(Notification.VISIBILITY_PRIVATE);
builder.setContentText(private);
break;
case R.id.rb_secret:
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setContentText(secret);
break;
default:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText(public);
break;
}
}
介紹 在谷歌的官網我們可以看到它是這樣介紹的:RecyclerView is a more advanced and flexible version of List
隨著Android設備增多,不少網站都開始設備Android設備,而Android主流設備類型以手機和平板為主。網站在適配時通過User Agent(用戶代理,以下簡稱U
手機QQ快速取消圖標上未讀消息的數字。現在手機QQ5.0也開始跟iso的一樣。在圖表顯示未讀消息的數字。我們進入手機QQ的時候,想取消那個未讀的顯示數字,就
本文為大家演示了如何使用Chronometer控件實現Android計時器的實例。先貼上最終的實現效果圖:Android計時器實現思路使用Chronometer控件實現計