Ste*_*yle 6 android android-notifications android-notification-bar android-2.3-gingerbread
这是我正在编写的一个应用程序,用于熟悉一些API,除了在Android中演示某些功能之外,它没有任何实际用途.我有一个Service在前台(startForeground)中运行,并且正在进行中Notification,当点击返回到应用程序时.的Service的广播,并记录它们的DB监听.
创建我的Notification使用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....
}
Run Code Online (Sandbox Code Playgroud)
当Service启动时,Notification和股票的文字都在Android上的所有版本显示:

我还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());
}
}
Run Code Online (Sandbox Code Playgroud)
为了测试更新Notification,我BroadcastReceiver听了时间刻录广播,并updateNotificationTicker用当前时间调用:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这完全适用于(几乎)每个版本的Android,因为自动收报机文本闪烁,内容文本更新于Notification:


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

NotificationCompat.Builder以某种方式设置我的方式导致Android 2.3.3上没有显示自动收报机文字?Notification吗?NotificationCompat.Builder在Android 2.3.3上无法正常工作的缺陷吗?| 归档时间: |
|
| 查看次数: |
7568 次 |
| 最近记录: |