不推荐使用的notification.setLatestEventInfo

时间:2017-07-25 14:05:44

标签: android notifications

我遇到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);

}

我想在新级api中使用此行“notification.flags | = 2”的标志。 我是新手android

1 个答案:

答案 0 :(得分: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);
 }