2016-01-22 51 views
0

我剛剛導入了一個android項目,並且在通知部分中,我必須使用setLatestEventInfo。但Android Studio說不能解決方法setLatestEventinfo。這裏是我的代碼片段,請您幫忙修改我的代碼,而回答無法在android通知中解析方法setLatestEventInfo

  NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0); 
      Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class); 
      notify_intent.putExtra("filename", "Special Service Report"); 
      notify_intent.putExtra("URL", urls[0]); 
      PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT); 
      notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Service Report", contentIntent); 
      notification.flags = Notification.FLAG_AUTO_CANCEL; 
      notification.defaults = Notification.DEFAULT_SOUND ; 
      nm.notify((int)System.currentTimeMillis(),notification); 
      } 
      else if(message.equalsIgnoreCase("Special Columnist Blog")) 
      { 

      NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0); 
      Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class); 
      notify_intent.putExtra("filename", "Special Columnist Blog"); 
      notify_intent.putExtra("URL", urls[1]); 
      PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT); 
      notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Columnist Blog", contentIntent); 
      notification.flags = Notification.FLAG_AUTO_CANCEL; 
      notification.defaults = Notification.DEFAULT_SOUND ; 
      nm.notify((int)System.currentTimeMillis(),notification); 
      } 
+0

同樣在這裏,儘管在Eclipse中,在代碼中我還沒有碰過一段時間,從那時起對Eclipse,ADT和Android SDK進行了一些更新。 API文檔沒有提及該方法 - 儘管該代碼用於運行。因此,這不可能是Android Studio的罪魁禍首。 – user149408

回答

0

對於最好的方法,從支持的Android工作室的創建通知代碼,

右鍵單擊應用並單擊然後UI組件然後通知然後單擊完成。

該代碼已準備好使用。

+0

單擊新建後確實找到UI組件 – Omega

+0

立即查看我的編輯 – Brendon

1

setLatestEventInfo()已在removed中從Notification在API 23中。如果您的目標是API 23或更高版本,則需要重新編寫代碼。

相反的:

Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0); 
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Columnist Blog", contentIntent); 

務必:

Notification.Builder builder = new Notification.Builder(context) 
.setSmallIcon(R.drawable.ic_launcher) 
.setContentTitle("CEPF Mobile") 
.setContentText("New post in Special Columnist Blog") 
.setContentIntent(contentIntent); 
Notification notification = builder.buid(); 

上使用的幾個注意事項:

  • 如果您使用Android支持庫,使用NotificationCompat.Builder代替Notification.Builder
  • 由於您在通知中設置了更多屬性,請查看構建器類提供的方法,並考慮使用這些方法,特別是在不同位置創建類似通知時。
  • 不推薦使用應用程序的通知主圖標,因爲Android現在強制通知圖標以黑白呈現,所有不透明區域都呈現爲白色。您可能會想要設計一個在此設置中仍可識別的圖標。
相關問題