How is the time format updated for Android?

I am using a custom RemoteView for AndroidNotification, and I would like to simulate system behavior.

How does Android update its notification time format - do they ever change after they are installed? How can I simulate this behavior?

+5
source share
4 answers

I'm not sure that you are still looking for answers, given that you yourself provided them. However, if you want to achieve your original goal, you probably want to

  • Restore RemoteView whenever time changes (it's just easier)
  • Set BroadcastReceiver to catch the ticks of the clock so you know when the time has changed.

So, some code is a bit like this:

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
source

, , DateUtils, formatSameDayTime, , , , .

+2

It is not possible to update a notification after adding unless .notify is called again with the same identifier.

If you use a timestamp, it is better to use your own Notification NotificationCompat.Builder without using RemoteViews.

+2
source

Every time you update your notification, do something simple (24 hours) ...

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
source

All Articles