在我的应用程序中,我想使用网络时间而不是设备的时间.我的应用程序的要求是时间应该是正确的.
我试图从NTS服务器获得时间,但加载器继续运行并且不停止我等待超过30分钟,但我仍然没有得到任何东西.
我想知道是否有任何其他方法来获取来自网络,因为我没有从网络中获取时间花费这么多时间.
public class MainActivity extends Activity {
public static final String TIME_SERVER = "time-a.nist.gov";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGetTime = (Button)findViewById(R.id.button1);
btnGetTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
new GetTimeFromNetwork().execute();
}
});
}
public class GetTimeFromNetwork extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
Log.i( "pre execute","yes");
}
@Override
protected String doInBackground(String... …Run Code Online (Sandbox Code Playgroud)