Truetime Android API,如何在打开activity时自动获取GMT,PST,设备时间的值?
Truetime Android API, how to get the values GMT, PST, device time automatic whenever open the activity?
我正在使用 API truetime-android。
https://github.com/instacart/truetime-android
我的问题是,如果我想获取 GMT、PST 和设备时间的值。我必须按下立即获取时间按钮。我不喜欢这样,我希望每当我打开 activity 时,GMT、PST、设备时间这三个已经有了值,所以我不需要按下按钮 Get Time Now 来获取 GMT 值, PST, 设备时间。如何实施?
这是我的代码
public class SampleActivity extends AppCompatActivity {
private Button refreshBtn;
private TextView timeGMT;
private TextView timePST;
private TextView timeDeviceTime;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
setupUI();
init(this);
click() ;
}
public static void init(final Context context) {
(new Thread(new Runnable() {
public void run() {
try {
TrueTime.build().withNtpHost("time.google.com").withLoggingEnabled(false).
withSharedPreferencesCache(context).
withConnectionTimeout(31428).initialize();
} catch (IOException var2) {
var2.printStackTrace();
}
}
})).start();
private void click() {
Date trueTime;
refreshBtn!!.setOnClickListener(View.OnClickListener {
if (!TrueTime.isInitialized()) {
//do nothing
} else {
trueTime = TrueTime.now();
Date deviceTime = new Date();
timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
}
}
}
private void setupUI() {
refreshBtn = (Button) findViewById(R.id.tt_btn_refresh);
timeGMT = (TextView) findViewById(R.id.tt_time_gmt);
timePST = (TextView) findViewById(R.id.tt_time_pst);
timeDeviceTime = (TextView) findViewById(R.id.tt_time_device);
}
private String formatDate(Date :date ,String : pattern,TimeZone : timeZone){
String format = SimpleDateFormat(pattern, Locale.ENGLISH);
format.timeZone = timeZone;
return format.format(date);
}
试试这个。您可以在 onCreate
中找到我的更改
public class SampleActivity extends AppCompatActivity {
private Button refreshBtn;
private TextView timeGMT;
private TextView timePST;
private TextView timeDeviceTime;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
setupUI();
init(this);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (TrueTime.isInitialized()) {
trueTime = TrueTime.now();
Date deviceTime = new Date();
timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
}
}
}, 1000);
}
public static void init(final Context context) {
(new Thread(new Runnable() {
public void run() {
try {
TrueTime.build().withNtpHost("time.google.com").withLoggingEnabled(false).
withSharedPreferencesCache(context).
withConnectionTimeout(31428).initialize();
} catch (IOException var2) {
var2.printStackTrace();
}
}
})).start();
private void click() {
Date trueTime;
refreshBtn!!.setOnClickListener(View.OnClickListener {
if (!TrueTime.isInitialized()) {
//do nothing
} else {
trueTime = TrueTime.now();
Date deviceTime = new Date();
timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
}
}
}
private void setupUI() {
refreshBtn = (Button) findViewById(R.id.tt_btn_refresh);
timeGMT = (TextView) findViewById(R.id.tt_time_gmt);
timePST = (TextView) findViewById(R.id.tt_time_pst);
timeDeviceTime = (TextView) findViewById(R.id.tt_time_device);
}
private String formatDate(Date :date ,String : pattern,TimeZone : timeZone){
String format = SimpleDateFormat(pattern, Locale.ENGLISH);
format.timeZone = timeZone;
return format.format(date);
}
我正在使用 API truetime-android。 https://github.com/instacart/truetime-android
我的问题是,如果我想获取 GMT、PST 和设备时间的值。我必须按下立即获取时间按钮。我不喜欢这样,我希望每当我打开 activity 时,GMT、PST、设备时间这三个已经有了值,所以我不需要按下按钮 Get Time Now 来获取 GMT 值, PST, 设备时间。如何实施?
这是我的代码
public class SampleActivity extends AppCompatActivity {
private Button refreshBtn;
private TextView timeGMT;
private TextView timePST;
private TextView timeDeviceTime;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
setupUI();
init(this);
click() ;
}
public static void init(final Context context) {
(new Thread(new Runnable() {
public void run() {
try {
TrueTime.build().withNtpHost("time.google.com").withLoggingEnabled(false).
withSharedPreferencesCache(context).
withConnectionTimeout(31428).initialize();
} catch (IOException var2) {
var2.printStackTrace();
}
}
})).start();
private void click() {
Date trueTime;
refreshBtn!!.setOnClickListener(View.OnClickListener {
if (!TrueTime.isInitialized()) {
//do nothing
} else {
trueTime = TrueTime.now();
Date deviceTime = new Date();
timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
}
}
}
private void setupUI() {
refreshBtn = (Button) findViewById(R.id.tt_btn_refresh);
timeGMT = (TextView) findViewById(R.id.tt_time_gmt);
timePST = (TextView) findViewById(R.id.tt_time_pst);
timeDeviceTime = (TextView) findViewById(R.id.tt_time_device);
}
private String formatDate(Date :date ,String : pattern,TimeZone : timeZone){
String format = SimpleDateFormat(pattern, Locale.ENGLISH);
format.timeZone = timeZone;
return format.format(date);
}
试试这个。您可以在 onCreate
中找到我的更改public class SampleActivity extends AppCompatActivity {
private Button refreshBtn;
private TextView timeGMT;
private TextView timePST;
private TextView timeDeviceTime;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
setupUI();
init(this);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (TrueTime.isInitialized()) {
trueTime = TrueTime.now();
Date deviceTime = new Date();
timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
}
}
}, 1000);
}
public static void init(final Context context) {
(new Thread(new Runnable() {
public void run() {
try {
TrueTime.build().withNtpHost("time.google.com").withLoggingEnabled(false).
withSharedPreferencesCache(context).
withConnectionTimeout(31428).initialize();
} catch (IOException var2) {
var2.printStackTrace();
}
}
})).start();
private void click() {
Date trueTime;
refreshBtn!!.setOnClickListener(View.OnClickListener {
if (!TrueTime.isInitialized()) {
//do nothing
} else {
trueTime = TrueTime.now();
Date deviceTime = new Date();
timeGMT.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT")));
timePST.setText(formatDate(trueTime, "yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone("GMT07:00")));
timeDeviceTime.setText(formatDate(deviceTime, "yyyy-MM-dd HH:mm:ss",TimeZone.getTimeZone("GMT-07:00"))));
}
}
}
private void setupUI() {
refreshBtn = (Button) findViewById(R.id.tt_btn_refresh);
timeGMT = (TextView) findViewById(R.id.tt_time_gmt);
timePST = (TextView) findViewById(R.id.tt_time_pst);
timeDeviceTime = (TextView) findViewById(R.id.tt_time_device);
}
private String formatDate(Date :date ,String : pattern,TimeZone : timeZone){
String format = SimpleDateFormat(pattern, Locale.ENGLISH);
format.timeZone = timeZone;
return format.format(date);
}