2013-03-02 24 views
5

我正在使用AndroidNotification的自定义RemoteView,并且我想模仿系统行为。如何更新Android通知时间格式?

Android如何更新其通知时间格式 - 它们在设置后是否更改过?我怎么能模仿这种行为?

+0

我我的问题的一部分回答了。但是,这并不能解决我的问题。 我有一个RemoteViews与自定义视图(使用setContent)。我希望在我的通知中显示一个日期或时间,模仿原生Android的行为。我会使用API​​调用(DateUtils.formatSameDayTime)实现此目的。问题在于,一旦在RemoteView上创建了setTextViewText并调用了notificationManager.notify,就是这样,文本是永久的。 我希望根据时间的推移动态改变日期或时间(超过24小时会显示一个日期)。请帮忙。 – 2013-03-05 17:16:18

回答

0

我不确定您是否仍在寻找答案,因为您自己提供了答案。但是,如果你正在寻找实现自己最初的目标,你可能会想

  • 重建远程视窗每当时间变化(这只是比较容易的方式)
  • 建立一个BroadcastReceiver捕捉时钟的滴答所以你知道什么时候改变了。

所以,有些代码有点像这样:

class MyCleverThing extends Service (say) { 

    // Your stuff here 

    private static IntentFilter timeChangeIntentFilter; 
    static { 
     timeChangeIntentFilter = new IntentFilter(); 
     timeChangeIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 
     timeChangeIntentFilter.addAction(Intent.ACTION_TIME_CHANGED); 
    } 

    // Somewhere in onCreate or equivalent to set up the receiver 
    registerReceiver(timeChangedReceiver, timeChangeIntentFilter); 

    // The actual receiver 
    private final BroadcastReceiver timeChangedReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 

     if (action.equals(Intent.ACTION_TIME_CHANGED) || 
      action.equals(Intent.ACTION_TIMEZONE_CHANGED)) 
     { 
      updateWidgets(); // Your code to rebuild the remoteViews or whatever 
     } 
    } 
}; 
+0

有点复杂但合法的解决方案。 – 2013-03-12 16:39:36

+0

这是 - 如果有一个不太复杂的做法(和它一直工作等等),我很乐意看到它。感谢剔和赏金,感谢它。 – 2013-03-12 16:41:46

2

我对通知时间格式不太了解,但是如果你想模仿他们的行为,应该看看DateUtils这个类,尤其是formatSameDayTime,我认为这些都是你所描述的。

2

除非使用相同的ID再次调用.notify,否则无法在添加后更新通知。

如果处理时间戳记,最好使用本机通知NotificationCompat.Builder而不使用RemoteViews。

0

每次更新您的通知,做这样简单的东西(24小时)...

public void updateNotifTime(RemoteViews customNotifView){ 
    Date currentTime = new Date(); 
    int mins = currentTime.getMinutes(); 
    String minString = ""; 
    if(mins<10){ 
     minString = "0"; 
    } 
    minString += mins; 
    customNotifView.setTextViewText(R.id.time, currentTime.getHours()+":"+minString); 
} 
+0

我希望通知根据时间流逝自行更新,就像本机一样。 – 2013-03-12 16:36:54