【轉】【Android】Android不同版本下Notification建立方法

來源:互聯網
上載者:User

標籤:etc   很多   函數   nbsp   use   pie   when   cto   注意   

使用 new Notification(int icon, CharSequence tickerText, long when)建構函式時,Eclipse卻提示:" The constructor Notification(int, CharSequence, long) is deprecated "

/*** Constructs a Notification object with the information needed to* have a status bar icon without the standard expanded view.** @param icon The resource id of the icon to put in the status bar.* @param tickerText The text that flows by in the status bar when the notification first* activates.* @param when The time to show in the time field. In the System.currentTimeMillis* timebase.** @deprecated Use {@link Builder} instead.*/@Deprecatedpublic Notification(int icon, CharSequence tickerText, long when){  this.icon = icon;  this.tickerText = tickerText;  this.when = when;}

在不同的版本下Notification使用有一些不同,涉及到Builder的使用。現在總結如下,希望對以後使用的程式員有所協助。

 
  低於API Level 11版本,也就是Android 2.3.3以下的系統中,setLatestEventInfo()函數是唯一的實現方法。前面的有關屬性設定這裡就不再提了,網上資料很多。

Intent  intent = new Intent(this,MainActivity);  PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  notification.setLatestEventInfo(context, title, message, pendingIntent);          manager.notify(id, notification); 

高於API Level 11,低於API Level 16 (Android 4.1.2)版本的系統中,可使用Notification.Builder來建構函式。但要使用getNotification()來使notification實現。此時,前面版本在notification中設定的Flags,icon等屬性都已經無效,要在builder裡面設定。

Notification.Builder builder = new Notification.Builder(context)              .setAutoCancel(true)              .setContentTitle("title")              .setContentText("describe")              .setContentIntent(pendingIntent)              .setSmallIcon(R.drawable.ic_launcher)              .setWhen(System.currentTimeMillis())              .setOngoing(true);  notification=builder.getNotification();

高於API Level 16的版本,就可以用Builder和build()函數來配套的方便使用notification了。

Notification notification = new Notification.Builder(context)             .setAutoCancel(true)             .setContentTitle("title")             .setContentText("describe")             .setContentIntent(pendingIntent)             .setSmallIcon(R.drawable.ic_launcher)             .setWhen(System.currentTimeMillis())             .build();

【注意點】:
    在構造notification的時候有很多種寫法,但是要注意,用
  Notification notification = new Notification();
  這種構建方法的時候,一定要加上notification.icon這個設定,不然,程式雖然不會報錯,但是會沒有效果。 

問題:

使用了Notification下的setLatestEventInfo()方法時,Eclipse卻提示:“ The method setLatestEventInfo(Context, String, String, PendingIntent) is undefined for the type Notification”!

/*** Sets the {@link #contentView} field to be a view with the standard "Latest Event"* layout.** <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields* in the view.</p>* @param context The context for your application / activity.* @param contentTitle The title that goes in the expanded entry.* @param contentText The text that goes in the expanded entry.* @param contentIntent The intent to launch when the user clicks the expanded notification.* If this is an activity, it must include the* {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires* that you take care of task management as described in the* <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back* Stack</a> document.** @deprecated Use {@link Builder} instead.* @removed*/@Deprecatedpublic void setLatestEventInfo(Context context,CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {Notification.Builder builder = new Notification.Builder(context);// First, ensure that key pieces of information that may have been set directly// are preservedbuilder.setWhen(this.when);builder.setSmallIcon(this.icon);builder.setPriority(this.priority);builder.setTicker(this.tickerText);builder.setNumber(this.number);builder.setColor(this.color);builder.mFlags = this.flags;builder.setSound(this.sound, this.audioStreamType);builder.setDefaults(this.defaults);builder.setVibrate(this.vibrate);builder.setDeleteIntent(this.deleteIntent);// now apply the latestEventInfo fieldsif (contentTitle != null) {builder.setContentTitle(contentTitle);}if (contentText != null) {builder.setContentText(contentText);}builder.setContentIntent(contentIntent);builder.buildInto(this);}

setLatestEventInfo方法已被removed。

原文地址:http://www.cnblogs.com/Arture/p/5523695.html

 

【轉】【Android】Android不同版本下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.