2017-07-25 210 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