通知的各种实用写法技巧
生活随笔
收集整理的這篇文章主要介紹了
通知的各种实用写法技巧
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? ? ?Notification的實用技巧
這里講下兩種寫法:廢話少說,直接上代碼。
第一種:
// 第一種彈出通知的方法@SuppressWarnings("deprecation")private void setpopnotification1() {// 首先拿到一個通知的管理者NotificationManager manager1 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 設(shè)置沒有下拉時顯示的圖標(biāo)和標(biāo)題int icon1 = R.drawable.ic_launcher;CharSequence tickerText = "您有新的預(yù)約";// 顯示的時間int when = (int) System.currentTimeMillis();// 過時的寫法Notification notification = new Notification(icon1, tickerText, when);// 定義下拉通知欄時要展現(xiàn)的內(nèi)容信息CharSequence contentTitle = "您的預(yù)約提醒";CharSequence contentText = "通知的內(nèi)容在這里顯示了哦";Intent notificationIntent = new Intent(this, MainActivity.class);// 配合SingleTask// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK// | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);notification.setLatestEventInfo(this, contentTitle, contentText,contentIntent);notification.flags = Notification.FLAG_AUTO_CANCEL;// 設(shè)置自動消失manager1.notify(1, notification);}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
第二種: // 第二種彈出通知的方法private void setpopnotification2() {NotificationManager manager2 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this);int icon = R.drawable.ic_launcher;// 設(shè)置沒有下拉時顯示的圖標(biāo)和標(biāo)題builder.setSmallIcon(icon);builder.setTicker("您有新的預(yù)約");// 設(shè)置點擊后自動消失builder.setAutoCancel(true);// 設(shè)置下拉后顯示的圖標(biāo)builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));// 下拉后顯示內(nèi)容標(biāo)題builder.setContentTitle("您的預(yù)約提醒");// 顯示的內(nèi)容builder.setContentText("通知的內(nèi)容在這里顯示了哦");// 顯示時間builder.setWhen(System.currentTimeMillis());// 設(shè)置通知的優(yōu)先級--Max代表不管手機目前有多少其他通知都能顯示在最上面builder.setPriority(Notification.PRIORITY_MAX);// 設(shè)置通知的鈴聲。震動。燈光// DEFAULT_ALL---默認(rèn)系統(tǒng)全部// DEFAULT_SOUND--鈴聲// DEFAULT_VIBRATE--震動// ...builder.setDefaults(Notification.DEFAULT_ALL);// 點擊能跳轉(zhuǎn)部分Intent intent = new Intent(this, MainActivity.class);// 設(shè)置flag---FLAG_ACTIVITY_NEW_TASK保證每次都是最新的(項目中很實用)// 當(dāng)點擊要跳轉(zhuǎn)時需要在清單文件中對activity進(jìn)行啟動模式設(shè)置--一般都是singleTaskintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);// 給每個通知設(shè)置唯一的requestCode,保證每次的通知不重復(fù),// 不然點擊時就會覆蓋之前的,點擊之前彈出的通知就沒有用/*** Flags為PendingIntent.FLAG_CANCEL_CURRENT,則只有最后一次PendingIntent有效,* 之前的都無效了。 PendingIntent.FLAG_UPDATE_CURRENT,則每次都是最新的。。具體的使用要看項目需求了*/int requestCode = (int) SystemClock.uptimeMillis();PendingIntent pendingIntent = PendingIntent.getActivity(this,requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);builder.setContentIntent(pendingIntent);// 設(shè)置顯示manager2.notify(1, builder.build());}
上述兩種都可以使用顯示通知出來,建議使用第二種,畢竟第一種已經(jīng)過時了。了解了基本用法結(jié)合項目需求來做會更好。
附上Demo地址:http://download.csdn.net/detail/ae_fring/9677156
總結(jié)
以上是生活随笔為你收集整理的通知的各种实用写法技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fanuc机器人空间信号解析
- 下一篇: 无记忆分布