Tecnologia do Blogger.
RSS

Re: [androidbrasil-dev] Re: Api Data e Hora

Vc pode usar um handler um para recuperar a hora da thread.

Em 28/12/2015 13:23, "Renato Souza" <renatovieiradesouza1@gmail.com> escreveu:
Valeu pelas dicas. Vou executar fora da thread, até sei fazer isso.

Daí, como posso recuperar o retorno de hora dessa função.

Abs

Em segunda-feira, 28 de dezembro de 2015 12:02:09 UTC-2, L. escreveu:
Você está colocando o seu método main dentro do método onCreate. Feche o método onCreate antes.
Outro detalhe é que a assinatura do main é public static void main(String[] args). Tá faltando o static na sua assinatura.

Em segunda-feira, 28 de dezembro de 2015 11:02:33 UTC-2, Renato Souza escreveu:
Pessoal, consegui os arquivos da classe SntpCliente, e estou com problemas para implementar a mesma no meu código.

A ideia é eu pegar a hora no inicio da minha aplicação, veja o trecho que faço isso e da erro no Public void main...

@Override
protected void onCreate(Bundle savedInstanceState) {
//Remove barra de titulo
requestWindowFeature(Window.FEATURE_NO_TITLE);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);

//Recupera data de acesso - Atenção- Datas em Java vão de 0 a 11, nesse caso fez-se necessário 
//adicionar +1 no momento do save no banco
Calendar calendar = Calendar.getInstance();
ano = calendar.get(Calendar.YEAR);
mes = calendar.get(Calendar.MONTH);
dia = calendar.get(Calendar.DAY_OF_MONTH);

//Recupera variaveis
idMatricula = (EditText)findViewById(R.id.idMatricula);
btnEntrar = (Button)findViewById(R.id.btnEntrar);
txtHoraGlobal = (TextView) findViewById(R.id.txtHoraGlobal);

helper = new BDCore(this);
                //O ERRO OCORRE AQUI NESSE main, ele diz "void is an invalid  type for the variable main"
public void main(String[] args) throws IOException {
final String serverName = args.length>0?args[0]:"us.pool.ntp.org";
final DatagramSocket socket = new DatagramSocket();
final InetAddress address = InetAddress.getByName(serverName);
final byte[] buffer = new NtpMessage().toByteArray();

DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, PORT);
socket.send(packet);

packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);

// Immediately record the incoming timestamp
final double destinationTimestamp = NtpMessage.now ();
final NtpMessage msg = new NtpMessage(packet.getData());
socket.close();

/* Presumably, msg.orginateTimestamp unchanged by server. */

// Formula for delay according to the RFC2030 errata
final double roundTripDelay =
(destinationTimestamp - msg.originateTimestamp) -
(msg.transmitTimestamp - msg.receiveTimestamp);

// The amount the server is ahead of the client
final double localClockOffset =
((msg.receiveTimestamp - msg.originateTimestamp) +
(msg.transmitTimestamp - destinationTimestamp)) / 2;


// Display response
System.out.format ("NTP server: %s%n", address);
System.out.printf ("Round-trip delay:   %+9.2f ms%n", 1000*roundTripDelay);
System.out.printf ("Local clock offset: %+9.2f ms%n", 1000*localClockOffset);

final long now = System.currentTimeMillis();  // milliseconds 1x10e-3 seconds
final long cor = now + Math.round (1000.0*localClockOffset);
System.out.printf ("Local time:      %1$ta, %1$td %1$tb %1$tY, %1$tI:%1$tm:%1$tS.%1$tL %1$tp %1$tZ%n", now);
System.out.printf ("Corrected time:  %1$ta, %1$td %1$tb %1$tY, %1$tI:%1$tm:%1$tS.%1$tL %1$tp %1$tZ%n", cor);

txtHoraGlobal.setText(now);
}

}

Em quinta-feira, 24 de dezembro de 2015 00:59:05 UTC-2, Renato Souza escreveu:
Alguém recomenda uma api para data e hora somente.

Abs

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário