Android 如何获取最后一次看到的功能
在过去的几天里,我尝试了安卓系统的“最后一次看到”功能,以获取用户活动的时间。。。。我研究了很多答案,但我不满意。。。 以下代码将查找此人在线的天数、周数或年数。。。。。。。 对于每一个新的android程序员来说,这只是小菜一碟Android 如何获取最后一次看到的功能,android,Android,在过去的几天里,我尝试了安卓系统的“最后一次看到”功能,以获取用户活动的时间。。。。我研究了很多答案,但我不满意。。。 以下代码将查找此人在线的天数、周数或年数。。。。。。。 对于每一个新的android程序员来说,这只是小菜一碟 GetTimeAgo GetTimeAgo=new GetTimeAgo(); long lastTime=long.parseLong(在线); 字符串lastSeenTime=getTimeAgo.getTimeAgo(lastTime,getApplicatio
GetTimeAgo GetTimeAgo=new GetTimeAgo();
long lastTime=long.parseLong(在线);
字符串lastSeenTime=getTimeAgo.getTimeAgo(lastTime,getApplicationContext());
mLastSeenView.setText(lastSeenTime)代码>
通过以上代码,您可以将其应用于任何活动
打开.class文件GetTimeAgo
并应用以下代码
public class GetTimeAgo extends Application {
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 double DAY_MILLIS = 24 * HOUR_MILLIS;
private static final double WEEK_MILLIS = 7 * DAY_MILLIS;
private static final double MONTH_MILLIS = DAY_MILLIS * 30;
private static final double YEAR_MILLIS = WEEK_MILLIS * 52;
public static String getTimeAgo(long time, Context ctx) {
if (time < 1000000000000L) {
// if timestamp given in seconds, convert to millis
time *= 1000;
}
long now = System.currentTimeMillis();
if (time > now || time <= 0) {
return null;
}
// TODO: localize
final long diff = now - time;
if (diff < MINUTE_MILLIS)
{
return "just now";
}
else if (diff < 2 * MINUTE_MILLIS)
{
return "a minute ago";
}
else if (diff < 50 * MINUTE_MILLIS)
{
double roundup = diff / MINUTE_MILLIS;
int b = (int)(roundup);
return b + " minutes ago";
}
else if (diff < 90 * MINUTE_MILLIS)
{
return "an hour ago";
}
else if (diff < 24 * HOUR_MILLIS)
{
double roundup = diff / HOUR_MILLIS;
int b = (int)(roundup);
return b + " hours ago";
}
else if (diff < 48 * HOUR_MILLIS)
{
return "yesterday";
}
else if (diff < 7 * DAY_MILLIS)
{
double roundup = diff / DAY_MILLIS;
int b = (int)(roundup);
return b + " days ago";
}
else if (diff < 2 * WEEK_MILLIS)
{
return "a week ago";
}
else if (diff < DAY_MILLIS * 30.43675)
{
double roundup = diff / WEEK_MILLIS;
int b = (int)(roundup);
return b + " weeks ago";
}
else if (diff < 2 * MONTH_MILLIS)
{
return "a month ago";
}
else if (diff < WEEK_MILLIS * 52.2)
{
double roundup = diff / MONTH_MILLIS;
int b = (int)(roundup);
return b + " months ago";
}
else if(diff < 2 * YEAR_MILLIS)
{
return "a year ago";
}
else
{
double roundup = diff / YEAR_MILLIS;
int b = (int)(roundup);
return b + " years ago";
}
}
}
public类GetTimeAgo扩展了应用程序{
专用静态最终整数秒\u毫秒=1000;
专用静态最终整数分钟=60*秒;
专用静态最终整数小时=60*分钟;
私人静态最终双日\u毫秒=24*小时\u毫秒;
私人静态最终双周=7*天;
私人静态最终双月=30天;
私人静态最终双年薪=周薪*52;
公共静态字符串getTimeAgo(长时间,上下文ctx){
如果(时间<10000000000L){
//如果时间戳以秒为单位,则转换为毫秒
时间*=1000;
}
long now=System.currentTimeMillis();
如果(时间>现在| |时间我不确定你是否想走这条路,但它可能会奏效
使用Firebase,您可以执行此操作。
如果您在应用程序中启用了实时数据库,每次打开应用程序时,您都可以使用设置数据库的日期。setValue(value)
其中value
类似
//Runs in your main loop
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference lastSeenData = database.getReference("message");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
lastSeenData.setValue(dateFormate.format(date));
您可能需要在setValue
语句中调用.toString
如果你想做一些像“某某最后一次被看见是在2d以前”,那么您可能需要获得某个用户登录的日期,然后从当前日期中减去它。我的想法是,更容易获得毫秒,用当前毫秒减去,然后将差值作为格式化日期吐出。实际上,当用户断开连接时,它会使用时间戳,计算当前时间以节省时间,并告诉time差异,并说明用户是否在1天1周甚至1个月和1年前在线