Skip to Main Content

How to use smallest time available?

HaydaNov 24 2016 — edited Nov 24 2016

Hello

i found chat app , very simple and I want to add the exact time in nanosecond  or millisecond before sending and after receiving the message, so I add these lines

System.nanotime();

but I always got zero !!

Could you please help me, thanks

package androidclient.example.com.chatclient;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.Socket;

import java.net.UnknownHostException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import android.app.TimePickerDialog;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import com.google.android.gms.appindexing.Action;

import com.google.android.gms.appindexing.AppIndex;

import com.google.android.gms.appindexing.Thing;

import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends Activity {

TextView textResponse, txt;

EditText editTextAddress, editTextPort;

Button buttonConnect, buttonClear;

EditText welcomeMsg;

/**

* ATTENTION: This was auto-generated to implement the App Indexing API.

* See https://g.co/AppIndexing/AndroidStudio for more information.

*/

private GoogleApiClient client;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextAddress = (EditText) findViewById(R.id.address);

editTextPort = (EditText) findViewById(R.id.port);

buttonConnect = (Button) findViewById(R.id.connect);

buttonClear = (Button) findViewById(R.id.clear);

textResponse = (TextView) findViewById(R.id.response);

txt = (TextView)findViewById(R.id.time) ;

welcomeMsg = (EditText) findViewById(R.id.welcomemsg);

buttonConnect.setOnClickListener(buttonConnectOnClickListener);

buttonClear.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

textResponse.setText("");

}

});

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

public OnClickListener buttonConnectOnClickListener = new OnClickListener() {

@Override

public void onClick(View arg0) {

String tMsg = welcomeMsg.getText().toString();

if (tMsg.equals("")) {

tMsg = null;

Toast.makeText(MainActivity.this, "No Welcome Msg sent", Toast.LENGTH_SHORT).show();

}

MyClientTask myClientTask = new MyClientTask(editTextAddress

.getText().toString(), Integer.parseInt(editTextPort

.getText().toString()),

tMsg);

myClientTask.execute();

txt.setText(Long.toString((myClientTask.res)/2));

}

};

/**

* ATTENTION: This was auto-generated to implement the App Indexing API.

* See https://g.co/AppIndexing/AndroidStudio for more information.

*/

public Action getIndexApiAction() {

Thing object = new Thing.Builder()

.setName("Main Page") // TODO: Define a title for the content shown.

// TODO: Make sure this auto-generated URL is correct.

.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))

.build();

return new Action.Builder(Action.TYPE_VIEW)

.setObject(object)

.setActionStatus(Action.STATUS_TYPE_COMPLETED)

.build();

}

@Override

public void onStart() {

super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

client.connect();

AppIndex.AppIndexApi.start(client, getIndexApiAction());

}

@Override

public void onStop() {

super.onStop();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

AppIndex.AppIndexApi.end(client, getIndexApiAction());

client.disconnect();

}

public class MyClientTask extends AsyncTask<Void, Void, Void> {

String dstAddress;

int dstPort;

String response = "";

String msgToServer;

long sec1, sec2,res;

MyClientTask(String addr, int port, String msgTo) {

dstAddress = addr;

dstPort = port;

msgToServer = msgTo;

res = sec2- sec2;

}

@Override

protected Void doInBackground(Void... arg0) {

Socket socket = null;

DataOutputStream dataOutputStream = null;

DataInputStream dataInputStream = null;

try {

socket = new Socket(dstAddress, dstPort);

dataOutputStream = new DataOutputStream(

socket.getOutputStream());

dataInputStream = new DataInputStream(socket.getInputStream());

if (msgToServer != null) {

dataOutputStream.writeUTF(msgToServer);

//mToast.show();*/

}

long sec1 = System.nanoTime();

response = dataInputStream.readUTF();

long sec2 = System.nanoTime();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

response = "UnknownHostException: " + e.toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

response = "IOException: " + e.toString();

} finally {

if (socket != null) {

try {

socket.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (dataOutputStream != null) {

try {

dataOutputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (dataInputStream != null) {

try {

dataInputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return null;

}

@Override

protected void onPostExecute(Void result) {

textResponse.setText(response);

super.onPostExecute(result);

}

}

}

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Dec 22 2016
Added on Nov 24 2016
2 comments
642 views