更新通知时NotificationCompat代码文本不显示

时间:2013-05-08 16:11:23

标签: android android-notifications android-notification-bar android-2.3-gingerbread

背景

这是我正在编写的应用程序,可以熟悉一些API,除了在Android中演示某些功能外,它还提供没有用途。我有一个Service正在前台运行(startForeground),并且有一个正在进行的Notification,当点击时返回应用程序。 Service侦听广播并将其记录到数据库。

启动服务,创建通知

我使用Notification的{​​{1}}中的NotificationCompat.Builder创建onCreate

Service

@Override public void onCreate() { super.onCreate(); Log.v(TAG, "onCreate"); // Get the notification manager to update notifications mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent openAppIntent = new Intent(this, MainActivity.class); openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent selectNotifPendingIntent = PendingIntent.getActivity(this, 0, openAppIntent, 0); // Build the notification to show mNotificationBuilder = new NotificationCompat.Builder(this) .setContentTitle("Monitor Service") .setContentText("Logging system events") .setTicker("Starting monitor service") .setSmallIcon(R.drawable.ic_stat_cloud) .setContentIntent(selectNotifPendingIntent) .setOnlyAlertOnce(false) .setPriority(NotificationCompat.PRIORITY_LOW) .setUsesChronometer(true); // Start service in foreground startForeground(MonitorService.NOTIFICATION_ID_FOREGROUND, mNotificationBuilder.build()); // more code.... } 开始时,所有Android版本都会显示Service和自动收报机文字:

Service notification startup on Android 2.3.3

接下来,更新通知

我还在Notification添加了回调interface,通过更改自动收录器文本和内容文本来更新Service

Notification

要测试更新@Override public void updateNotificationTicker(String newTickerText) { Log.d(TAG, "updateNotificationTicker"); if (mNotificationBuilder != null && mNotificationManager != null) { mNotificationBuilder.setTicker(newTickerText); mNotificationBuilder.setContentText(newTickerText); mNotificationManager.notify(NOTIFICATION_ID_FOREGROUND, mNotificationBuilder.build()); } } ,我让Notification收听时间刻录广播,并使用当前时间拨打BroadcastReceiver

updateNotificationTicker

这几乎适用于每个版本的Android,因为自动收报机文字闪烁,内容文字在 else if (action.equals(android.content.Intent.ACTION_TIME_TICK)) { SimpleDateFormat sdf = new SimpleDateFormat("h:mm aa", Locale.US); String message = "The time is now " + sdf.format(new Date()); newLogEntry.setMessage(message); // Update the ticker notification with the time if (mNotificationCallback != null) { mNotificationCallback.updateNotificationTicker(message); } } 上更新:

  • Android 2.2(API 8)
  • Android 4.0.3(API 15)
  • Android 4.1.2(API 16)
  • Android 4.2.2(API 17)

Android 2.2

Android 4.0.3

Android 2.3.3上的不同行为

在Android 2.3.3(API 10)模拟器或设备上运行时,首次启动Notification时会显示自动收报机文本(在第1个屏幕截图中显示:“启动监控服务”)。但是,更新Service时,即使内容文本确实更新,也不会显示自动收录器文本。

Android 2.3.3

问题

  • 我是否设置了Notification,导致Android 2.3.3上没有显示自动收报机文字?
  • 或者,在更新正在进行的NotificationCompat.Builder时,Android 2.3.3在显示自动收录器文字时的行为与其他版本有何不同?
  • 或者,这是Notification在Android 2.3.3上无法正常运行的缺陷吗?

0 个答案:

没有答案
相关问题