2017-07-25 213 views
-1

我有setLatestEventInfo問題,我無法將其更改爲新版本。 請幫我重寫新的代碼。已棄用notification.setLatestEventInfo

private void showNotification(int moodId, int textId) { 
    CharSequence text = getText(textId); 
    Notification notification = new Notification(R.drawable.icon, getText(R.string.notify_message_started), System.currentTimeMillis()); 
    notification.setLatestEventInfo(this, getText(R.string.notify_message_running_headline), getText(R.string.notify_message_running_text), 
      PendingIntent.getActivity(this, 0, new Intent(this, AudioMonitor.class).setFlags(603979776), 0)); 

    notification.flags |= 2; 
    this.mNM.notify(MOOD_NOTIFICATIONS, notification); 

} 

我想可以使用標誌爲此行「notification.flags | = 2」在新的級別api。 我是新的android初學者

+1

使用[NotificationCompat.Builder](https://developer.android.com/reference/android /app/Notification.Builder.html)而不是 – ADM

+1

[如何使用NotificationCompat.Builder創建通知?](https://stackoverflow.com/questions/13902115/how-to-create-a-notification-with -notificationcompat-builder) –

+0

@TadijaBagarić我想在通知中使用標誌。這個鏈接dos'nt有這個項目 – zahra

回答

0

可以爲API級別進行檢查,然後使用最新版本

final int sdkVersion = Integer.parseInt(Build.VERSION.SDK); 

if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) 
{ 
    // Deprecated in API level 11 
    CharSequence text = getText(textId); 
    Notification notification = new Notification(R.drawable.icon, 
    getText(R.string.notify_message_started), System.currentTimeMillis()); 
    notification.setLatestEventInfo(this, getText(R.string.notify_message_running_headline), getText(R.string.notify_message_running_text), 
     PendingIntent.getActivity(this, 0, new Intent(this, AudioMonitor.class).setFlags(603979776), 0)); 

    notification.flags |= 2; 
    this.mNM.notify(MOOD_NOTIFICATIONS, notification); 

} 
else 
{ 
// available from API level 11 and onwards 
final Intent notificationIntent = new Intent(this, AudioMonitor.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent contentIntent = PendingIntent.getActivity(myContext, 0, notificationIntent, 0); 

Notification notification = new Notification.Builder(mContext) 
    .setContentTitle(getText(R.string.notify_message_running_headline)) 
    .setContentText(getText(R.string.notify_message_running_text)) 
    .setContentIntent(contentIntent); 
    .setWhen(System.currentTimeMillis()) 
    .setOngoing(true) 
    .setSmallIcon(R.drawable.icon) 
    .setLargeIcon(R.drawable.icon) 
    .build(); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    notificationManager.notify(MOOD_NOTIFICATIONS, notification); 
} 
+0

謝謝你,但它不完整。我想標誌和System.currentTimeMillis())和..。根據我自己的代碼。你可以編輯你的代碼「可從API級別11及以上」嗎? – zahra

+0

如果你想要當前的時間,你不需要一個,但我仍然添加了代碼。至於旗幟,你現在有不同的標誌不同的方法,所以只是數字不會。現在,我已經使用FLAG INT 2在代碼中添加了正在執行的代碼。如果您想添加更多標誌,可以在這裏查看它們各自的方法。 https://developer.android.com/reference/android/app/Notification.Builder.html –

+0

我不需要使用上述代碼的其他部分? PendingIntent.getActivity(this,0,new Intent(this,AudioMonitor.class).setFlags(603979776),0));和this.mNM.notify(MOOD_NOTIFICATIONS,通知); – zahra