I am new in android and i ask a question about my thread that i create. I think it is stupid question but I am sorry.I have a onClick button listener. Its job is get the URL download link and stores in a variable.
/**
* this method invoke from setPositiveButton's dialog
*
* @param rootView
*/
private void addURLToList(View rootView) {
editTextAddURL = (EditText) rootView.findViewById(R.id.editText_add_url);
Log.i("===", "addURLToList: " + editTextAddURL.getText());
stringUrl = editTextAddURL.getText().toString();
*start GetSizeOfFile thread for getting size file and store
* in lenghtOfFile variable
*/
new GetSizeOfFile().start();
Log.i("====", "size of file after Thread: " + lenghtOfFile);
}
I create a Thread because I want to get file size.
private class GetSizeOfFile extends Thread {
@Override
public void run() {
super.run();
try {
URL url = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
lenghtOfFile = connection.getContentLength();
Log.i("====", "size of file in Thread: " + lenghtOfFile);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
everything is ok but when thread is started ,after few second my lenghtOfFile variable is initialized and I got 0 in lenghtOfFile
in this Line Log.i("====", "size of file after Thread: " + lenghtOfFile);
this is my logcat:
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/===: addURLToList: http://dl2.soft98.ir/soft/a/Adobe.Shockwave.Player.12.2.7.197.IE.rar
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/====: file name : Adobe.Shockwave.Player.12.2.7.197.IE.rar
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/====: size of file after Thread: 0
02-22 10:02:36.544 11333-11495/com.example.manifest.simplefiledownloadmanager I/====: size of file in Thread: 13524394
I want to get file's size from thread first.is it correct that I have to sleep the thread or exit standard way?sorry I am new in android