锁屏、切到后台 程序强制暂停时使用
private long leaveTime;
private void OnApplicationFocus(bool focus)
{
if (focus==false)
{
// 暂时APP切换到后台
this.leaveTime = GetTimeStamp;
}
else
{
// 再次进入的时间
long now = GetTimeStamp;
if (this.leaveTime>0&&now -this.leaveTime>3)
{
//退出
}
}
}
private void OnApplicationPause(bool pause)
{
if (pause)
{
// app 暂停
this.leaveTime = GetTimeStamp;
}
else
{
long now = GetTimeStamp;
if (this.leaveTime > 0 && now - this.leaveTime > 3)
{
//退出
}
}
}
转换自 1970.1.1至今的时间
using System;
static readonly DateTime DataTime1970 = new DateTime(1970, 1, 1);
// 返回时间戳(秒)
public static long GetTimeStamp
{
get
{
TimeSpan ts = DateTime.UtcNow - DataTime1970;
return Convert.ToInt64(ts.TotalSeconds);
}
}
/// 返回时间戳毫秒
public static long GetTimeStapMs
{
get
{
TimeSpan ts = DateTime.UtcNow - DataTime1970;
return Convert.ToInt64(ts.TotalMilliseconds);
}
}