Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Truetime Android API,如何在每次打开活动时自动获取GMT、PST和设备时间的值?_Java_Android_Datetime_Time_Current Time - Fatal编程技术网

Java Truetime Android API,如何在每次打开活动时自动获取GMT、PST和设备时间的值?

Java Truetime Android API,如何在每次打开活动时自动获取GMT、PST和设备时间的值?,java,android,datetime,time,current-time,Java,Android,Datetime,Time,Current Time,我正在使用API truetime android。 我的问题是,如果我想得到GMT、PST和设备时间的值。我得按一下按钮,现在就开始计时。我不喜欢这样,我希望每当我打开那个活动时,三个GMT,PST,设备时间都已经有了值,所以我现在不需要按“获取时间”按钮获取值GMT,PST,设备时间。如何实现这一点? 这是我的密码 public class SampleActivity extends AppCompatActivity { private Button refreshBtn; priv

我正在使用API truetime android。

我的问题是,如果我想得到GMT、PST和设备时间的值。我得按一下按钮,现在就开始计时。我不喜欢这样,我希望每当我打开那个活动时,三个GMT,PST,设备时间都已经有了值,所以我现在不需要按“获取时间”按钮获取值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);
}

好吧,如果我第一次打开SampleActivity,它没有显示任何内容。但是,如果我再次打开SampleActivity,它就会显示值。怎么解决?谢谢,我真的很感激。你能解释一下为什么添加处理程序会使代码工作吗?有时候在UI初始化之前会调用代码。这是延迟代码执行的一种方法。或者可以在onResume中调用无处理程序。那可能行得通。如果你觉得我的答案有用,就把我的答案标记为已接受。