如何为google messenger等聊天应用制作时间戳

时间:2017-02-06 13:03:45

标签: java android

我想在我的聊天应用中显示时间戳并更新此消息如果消息在2分钟之前发送,则在2分钟前显示时间戳我使用此方法显示消息发送时的时间戳但我想每秒更新一次

Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
timeStamp = simpleDateFormat.format(calendar.getTime());

3 个答案:

答案 0 :(得分:1)

您必须使用ListView或RecyclerView,您只需在适配器中使用以下方法在视图中设置时间。

注意:这将为您提供调用此方法的更新时间,如果用户继续上下滚动,您将获得最新更新时间,如果用户不在此情况下滚动它不会给你最新的时间。

/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;


public static String getTimeAgo(long time, Context ctx) {
    if (time < 1000000000000L) {
        // if timestamp given in seconds, convert to millis
        time *= 1000;
    }

    long now = getCurrentTime(ctx);
    if (time > now || time <= 0) {
        return null;
    }

    // TODO: localize
    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return MINUTE_MILLIS/SECOND_MILLIS;
    } else if (diff < 2 * MINUTE_MILLIS) {
        return "a minute ago";
    } else if (diff < 50 * MINUTE_MILLIS) {
        return diff / MINUTE_MILLIS + " minutes ago";
    } else if (diff < 90 * MINUTE_MILLIS) {
        return "an hour ago";
    } else if (diff < 24 * HOUR_MILLIS) {
        return diff / HOUR_MILLIS + " hours ago";
    } else if (diff < 48 * HOUR_MILLIS) {
        return "yesterday";
    } else {
        return diff / DAY_MILLIS + " days ago";
    }
}


使用Sangharsh建议的DateUtils's getRelative *函数之一。

答案 1 :(得分:1)

使用DateUtils getRelative*个{{3}}函数之一。

答案 2 :(得分:0)

每秒更新一次时间戳是不必要的。我建议你每分钟都这样做。对于您的聊天应用,我假设您有收到消息的时间。 在您的活动或片段中,创建一个广播接收器并使用默认的ACTION_TIME_TICK Intent每分钟接收更新,如下所示。我建议你把时间作为UNIX标记。

BroadcastReceiver mBroadcastReceiver;
long messageTime = message.getTime() // get your time from the message.
TextView textView;

@Override
public void onStart() {
    super.onStart();
    mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
                    textView.setText((System.currentTimeMillis-messageTime)/1000L);
        }
    };



 registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}


@Override
public void onStop() {
super.onStop();
if(mBroadcastReceiver != null) 
unregisterReceiver(mBroadcastReceiver);
编辑:我很着急。我认为你可以提出你的逻辑来转换你得到的基于上下文的文本的秒差。此代码在更新后显示时间为秒。