My code have more than one thread and Runnable. My problem is i change the value of a certain variable in the thread that the Runnable calling .
After the calling i make a check on that variable value but the value was not retrieved yet.
How can i retrieve the value after the processing? Here is the Runnable and the Thread code:
final Runnable r = new Runnable()
{
public void run()
{
if(flag==true)
onSwipe();
if(SwipeAgain==true)
handler.postDelayed(this, 1000);
}
};
private void onSwipe() {
new Thread() {
public void run() {
String data = null;
decryption_data = null;
encryption_data = null;
SwipeAgain=false;
handler.post(clear_encryption);
try {
data = sreader.ReadCard(15000);
} catch (Exception ex) {
if (ex instanceof TimeoutException) {
return;
} else
CloseSinWave();
}
if (data == null) {
SwipeAgain=true;
encryption_data = sreader.GetErrorString();
if (encryption_data.equalsIgnoreCase("cancel all"))
return;
handler.post(display_encryptiondata);
} else {
encryption_data = "\n" + data;
handler.post(display_encryptiondata);
}.start();
}
SwipeAgain is the value i want after processing
You have to use Callable, Runnable interface do not pass values to the parent method.
See this example.
You may require to use Generic Objects
Use a MONITOR final Object to wait and notify it when processing is done.
private final MONITOR Object[] = new Object[0];
private AtomicBoolean ready = new AtomicBoolean(false);
final Runnable r = new Runnable() {
public void run()
{
if(flag==true){
ready.set(false);
onSwipe();
synchronized(MONITOR){
if(!ready.get()){
try{
MONITOR.wait(); //will block until it get notified
}catch(InteruptedException e){}
}
}
}
if(SwipeAgain==true)
handler.postDelayed(this, 1000);
}
};
private void onSwipe() {
new Thread() {
public void run() {
try{
String data = null;
decryption_data = null;
encryption_data = null;
SwipeAgain=false;
handler.post(clear_encryption);
try {
data = sreader.ReadCard(15000);
} catch (Exception ex) {
if (ex instanceof TimeoutException) {
return;
} else
CloseSinWave();
}
if (data == null) {
SwipeAgain=true;
encryption_data = sreader.GetErrorString();
if (encryption_data.equalsIgnoreCase("cancel all"))
return;
handler.post(display_encryptiondata);
} else {
encryption_data = "\n" + data;
handler.post(display_encryptiondata);
}finally{
synchronized(MONITOR){
ready.set(true);
MONITOR.notifyAll(); //notify (and so unblock r.run())
}
}
}.start();
}
Related
I'm work on crate server and android client
but thread doesn't interrupted by android client
My android Client has request for the RoomList from server
and Server receive, Client Accept
and fill in my ListView used the Adapter
here's problem if Click backButton,
than RoomListAcitivity was close and thread was stop
but thread dosen't stop just alive in my app
first Enter has work on sucessfully
but Press BackButton on and re-Enter this Activity
MyApp just White, No Action
how can i stop this thread?
(and sorry for my English skill...)
i tried .interrupt() method , and handler.removeMessages(0)
but failed thread keep alive
upload this full java code just in case...
ListView roomList;
RoomAdapter roomAdapter;
Socketservice ss;
String msg,rtitle;
String msgs[];
String list[];
Thread listthread,EnterRoomThread,removeV;
boolean staterun = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_room);
roomList = findViewById(R.id.roomList);
roomAdapter = new RoomAdapter();
listthread = new Thread() {
public void run(){
ss.out.println("163|");
ss.out.println("100|");
try {
while (staterun == true) {
msg = ss.in.readLine();
handler.post(new Runnable() {
public void run() {
msgs = msg.split("\\|");
String protocol = msgs[0];
switch (protocol) {
case "163":
list = msgs[1].split(",");
for (int i = 0; i < list.length; i++) {
String list1[] = list[i].split("-");
String listT = list1[0];
int listC = Integer.parseInt(list1[1]);
int listI = Integer.parseInt(list1[2]);
roomAdapter.CreateRoom(listI, listT, listC);
}
roomList.setAdapter(roomAdapter);
msg = "";
msgs = null;
break;
case "200":
Intent i = new Intent(getApplicationContext(), GameWaitingActivity.class);
i.putExtra("tname", rtitle);
staterun = !staterun;
Toast.makeText(getApplicationContext(),""+listthread.isAlive(),Toast.LENGTH_SHORT).show();
startActivity(i);
finish();
break;
case "201":
Toast.makeText(getApplicationContext(), "방이 꽉 찼습니다.", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
} catch (IOException e) {
}
}
};
listthread.start();
roomList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Room item = (Room) roomList.getItemAtPosition(position);
rtitle=item.getTitle();
EnterRoomThread = new Thread() {
public void run() {
ss.out.println("200|" + rtitle);
EnterRoomThread.interrupt();
}
};
EnterRoomThread.start();
}
});
}
Handler handler = new Handler();
#Override
public void onBackPressed() {
removeV = new Thread() {
public void run(){
ss.out.println("101|");
removeV.interrupt();
}
};
removeV.start();
handler.removeMessages(0);
staterun = false;
listthread.interrupt();
Toast.makeText(getApplicationContext(),""+listthread.isAlive(),Toast.LENGTH_SHORT).show();
finish();
}
}
Go ahead with this, write this inside run() method
//use this boolean value to keep track.
boolean shouldRunFlag = true;
while (shouldRunFlag) {
try {
Thread.sleep(10);
//Do your work............
} catch (Exception e) {
e.printStackTrace();
Log.v(TAG, "Interrupted Exception caught");
// change the flag, so that while loop can be broken.
shouldRunFlag = false;
Log.v(TAG, "run: breaking the loop");
break;
}
}
and this is how you interrupt and clean the thread
private void interrupt(Thread currentThread) {
Log.i(TAG, "interrupt");
if (currentThread != null) {
Thread dummyThread = currentThread;
dummyThread.interrupt();
currentThread = null;
}
}
We have an main activity with a Text View which is requested to auto-retrieve data from TCP server in LAN.
We enabled AsyncTask in updateData() to call Connect to call Handler to update TextView named mTvVoc, and refresh connection by beginConnect().
The problem is the mTvVoc is not updated, it seems the AsyncTask does not function as expected?
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateData();
}
public void updateData(final String order) {
Connect(HOST, PORT, order);
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
System.out.println("get data from order--->" + order);
beginConnect(HOST, PORT, order);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}.execute();
}
public String Connect(final String HOST, final int PORT, final String OderType) {
new Thread(new Runnable() {
#Override
public void run() {
try {
socket = new Socket(HOST, PORT);
bufferedInputStream = new BufferedInputStream(socket.getInputStream());
System.out.println("bufferedInputStream--->" + bufferedInputStream);
printWriter = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
printWriter.println(Integer.toHexString(Integer
.parseInt(OderType)));
}
}
while (true) {
if (!socket.isClosed() && socket.isConnected() && !socket.isInputShutdown()) {
int temp = 0;
byte[] buf = new byte[1024];
while ((temp = bufferedInputStream.read(buf)) != -1) {
isConn(socket);
Log.d("Data received", new String(buf, 0, temp));
acceptinfo = new String(buf, 0, temp);
System.out.println("Data received====》" + acceptinfo);
Message message = new Message();
message.what = 1;
message.obj = acceptinfo;
handler.sendMessage(message);
}
}
}
} catch (UnknownHostException e) {
System.out.println("UnknownHostException-->connection failed");
stopConnect();
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException-->connection failed");
stopConnect();
e.printStackTrace();
}
}
}).start();
return acceptinfo;
}
public String beginConnect(final String HOST, final int PORT, final String OderType) {
new Thread(new Runnable() {
#Override
public void run() {
try {
socket = new Socket(HOST, PORT);
bufferedInputStream = new BufferedInputStream(socket.getInputStream());
System.out.println("bufferedInputStream--->" + bufferedInputStream);
printWriter = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
printWriter.println(Integer.toHexString(Integer.parseInt(OderType)));
}
}
} catch (UnknownHostException e) {
System.out.println("UnknownHostException-->connection failed");
stopConnect();
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException-->connection failed");
stopConnect();
e.printStackTrace();
}
}
}).start();
return acceptinfo;
}
public Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.i("message", msg.obj.toString());
if (msg.what == 0) {
mIvContent.setImageResource(R.drawable.wangluo_gray);
Toast.makeText(MainActivity.this, "Server connection failed", Toast.LENGTH_SHORT).show();
reGetData();
}
if (msg.what == 1) {
String info = msg.obj.toString();
if (info.length() != 18) {
mIvContent.setImageResource(R.drawable.wangluo_gray);
System.out.println("data verification failed");
reGetData();
return;
}
String iszeroupString = info.substring(1, 3);
String temperature = Integer.toString(Integer.parseInt(info.substring(3, 5), 16));
String vocValue = Integer.toString(Integer.parseInt(info.substring(7, 9), 16));
mTvVoc.setText(getVOC((Integer.valueOf(vocValue ) / 100.00)));
}
}
}
You cannot directly communicate from background thread to UI thread to
update any UI components.
Use this in your background thread to update textview from AsyncTask.
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Log.d("UI thread", "I am the UI thread");
// update your text here
}
});
To update on UI, you need to run the codes in UIThread. See code below and put in your handler:
runOnUiThread(new Runnable() {
#Override
public void run() {
Log.i("message", msg.obj.toString());
if (msg.what == 0) {
mIvContent.setImageResource(R.drawable.wangluo_gray);
Toast.makeText(MainActivity.this, "Server connection failed", Toast.LENGTH_SHORT).show();
reGetData();
}
if (msg.what == 1) {
String info = msg.obj.toString();
if (info.length() != 18) {
mIvContent.setImageResource(R.drawable.wangluo_gray);
System.out.println("data verification failed");
reGetData();
return;
}
String iszeroupString = info.substring(1, 3);
String temperature = Integer.toString(Integer.parseInt(info.substring(3, 5), 16));
String vocValue = Integer.toString(Integer.parseInt(info.substring(7, 9), 16));
mTvVoc.setText(getVOC((Integer.valueOf(vocValue ) / 100.00)));
}
}
});
Pretty bad to code that handler to update the gui. Do away with it as AsyncTask provides already the functionality to do so.
You should have looked better at the many examples.
Use publishProgress() and onProgressUpdate() to update a progress bar and/or other gui elements.
And do not wrap your code in a thread if it has to be executed in doInBackground().
I got problem with my asyncTask. I have my custom USB Scanner. I want to turn it on and off with ToggleButton. Scanning works fine but asynctask completly blocks user interface. I can't do nothing. Maybe you know what can I do to make it works better ?
Here's toggleButton :
mScanLayout.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
task.execute();
if(!isChecked)
task.cancel(true);
}
});
Here is asynctask :
public class scanAsyncTask extends AsyncTask<Void,Void,Void> {
#Override
protected Void doInBackground(Void... params) {
while(!isCancelled()) {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
adapter = new PartAdapter(getContext(), R.layout.part_item, mParts, mActivity,this);
adapter.startScanning();
}
});
}
return null;
}
}
And this is scanning method from adapter :
public void startScanning(){
final PendingIntent mPermissionIntent = PendingIntent.getBroadcast(getContext(), 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
getContext().registerReceiver(usbReceiver, filter);
UsbManager usbManager = (UsbManager) getContext().getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
UsbDevice device = null;
while (deviceIterator.hasNext()) {
device = deviceIterator.next();
if (device.getVendorId() == 1659 && device.getProductId() == 8963) {
this.device = device;
usbManager.requestPermission(device, mPermissionIntent);
break;
}
}
final UsbDevice finalDevice = device;
final UsbDevice finalDevice1 = device;
UsbConnector.CallbackListener listener = new UsbConnector.CallbackListener() {
#Override
public void onStatusChanged(UsbConnector.Status newStatus) {
Toast.makeText(getContext(), "status: " + newStatus, Toast.LENGTH_SHORT).show();
}
#Override
public void onScanCompleted(String result) {
Toast.makeText(getContext(), "result: " + result, Toast.LENGTH_SHORT).show();
}
};
UsbConnector connector = new UsbConnector(getContext(), finalDevice1,listener);
connector.run();
UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(finalDevice);
UsbSerialDevice serial = UsbSerialDevice.createUsbSerialDevice(finalDevice, usbDeviceConnection);
serial.open();
serial.setBaudRate(57600);
if (finalDevice1 != null) {
connector.run();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
connector.send(pal);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] received = connector.receive(36);
if (received == null) {
Toast.makeText(getContext(), "BĹ‚Ä…d inicjalizacji skanera", Toast.LENGTH_SHORT).show();
}
if (received != null) {
String response = null;
long longValue = ByteBuffer.wrap(received).getLong();
response = Long.toHexString(longValue).toUpperCase();
if (response.contains("DAAD0674016F6B26")) {
connector.send(readId);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] receivedTag = connector.receive(36);
if (receivedTag != null) {
String tag = null;
long tagValue = ByteBuffer.wrap(receivedTag).getLong();
tag = Long.toHexString(tagValue).toUpperCase();
if (tag.contentEquals("DAAD046F62ADA900")) {
startScanning();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (!tag.contains("DAAD046F62ADA900")) {
String tag2 = null;
long tagValue2 = ByteBuffer.wrap(receivedTag).getLong();
tag2 = Long.toHexString(tagValue2).toUpperCase();
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getContext(), notification);
r.play();
int i = 0;
for (Part part : mParts) {
if(part.getCode().contains(tag2)) {
part.setScan(true);
part.setScanCounter(part.getScanCounter() + 1);
i++;
notifyDataSetChanged();
}
}
if(i==0){
Intent intent = new Intent(getContext(),AddActivity.class);
intent.putExtra("tag",tag2);
mActivity.startActivityForResult(intent,2);
}
}
}
}
notifyDataSetChanged();
}
} else {
Toast.makeText(getContext(), R.string.plug_scanner, Toast.LENGTH_SHORT).show();
}
}
Please, help.
in your doInBackground you do:
mActivity.runOnUiThread(new Runnable() {
That defeats the purpose and you do not execute on the background anymore - you are on the main-thread and so block the UI
This Line of your code
mActivity.runOnUiThread(new Runnable() {
Runs on UI thread you should return result in doinbackground and then use it in onPostExecute which runs on UI thread. doInBackground is made to run on background not on UI thread but you forcing it to run on UI thread
My program is running fine in most of the andriod phone. But recently it found errors in some particular andriod phones. I found the error should be caused by the AsyncTask. The onPostExecute is not called. Please help. Thanks a lot!!
public void onCreate(Bundle savedInstanceState) {
this.setDebugTag(DEBUGTAG);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
try {
final View v_splashLayout = findViewById(R.id.splashLayout);
if(v_splashLayout== null){
FunctionUtil.logD(DEBUGTAG, " v_splashLayout :");
}
v_splashLayout.post(new Runnable() {
#Override
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
}
});
} catch(Exception e) {
FunctionUtil.logE(DEBUGTAG,"v_splashLayout error", e);
}
// init circleProgressBar
pb1 = (ProgressBar) findViewById(R.id.circleProgressBar);
dbh = (DatabaseHelper) OpenHelperManager.getHelper(this, DatabaseHelper.class);
if (FunctionUtil.ISDEBUGGING) {
checkSystemConfig();
//checkdb
}
init_neutralButtonOnClickListener();
init_quitButtonOnClickListener();
uiHandler = new MainHandler();
callAsyncGetLatestUpdateWS();
} // END, onCreate
private void callAsyncGetLatestUpdateWS() {
// check internet connection
// if isOnline, check the local db version with the server version
// if there is update, download and update the local db
boolean isOnline = FunctionUtil.checkInternetConnection(this);
boolean isBackgroundEnabled = FunctionUtil.checkBackgroundData(this);
FunctionUtil.logD(DEBUGTAG, "callAsyncGetLatestUpdateWS - isOnline="+isOnline+" , isBackgroundEnabled="+isBackgroundEnabled);
if (isOnline && isBackgroundEnabled) {
try {
// call webservice and update status
// call async task to do
if (checkVersionTask==null) {
checkVersionTask = new CheckVersionFromWS_Task();
}
if (checkVersionTask.getStatus().equals(AsyncTask.Status.PENDING)) {
// AsyncTask.Status.PENDING
checkVersionTask.execute();
} else if (checkVersionTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
// AsyncTask.Status.FINISHED
checkVersionTask.cancel(true);
checkVersionTask = null;
checkVersionTask = new CheckVersionFromWS_Task();
checkVersionTask.execute();
} else {
// AsyncTask.Status.RUNNING
//checkVersionTask.cancel(true);
}
} catch(Exception e) {
FunctionUtil.logE(DEBUGTAG, "callAsyncGetLatestUpdateWS error:", e);
}
// 2. go to MainActivity, temp enable
//goNext();
} // END, if isOnline
else {
if (!isOnline) {
// no connection
//showDialog(Constant.DIALOG_NOCONNECTION);
AlertDialogFactory.show(SplashActivity.this, Constant.DIALOG_NOCONNECTION, neutralButtonOnClickListener, true);
} else if (!isBackgroundEnabled) {
//no background
//showDialog(Constant.DIALOG_NOBACKGROUNDDATA);
AlertDialogFactory.show(SplashActivity.this, Constant.DIALOG_NOBACKGROUNDDATA, neutralButtonOnClickListener, true);
}
}
} // END, callAsyncGetLatestUpdateWS
private class CheckVersionFromWS_Task extends AsyncTask<Void, Void, LatestUpdateResult> {
// task start
#Override
protected void onPreExecute() {
FunctionUtil.logD(DEBUGTAG,getClass().getSimpleName() + " onPreExecute");
if (pb1!=null) {
pb1.setIndeterminate(true);
pb1.setVisibility(View.VISIBLE);
} else {
pb1 = (ProgressBar) findViewById(R.id.circleProgressBar);
pb1.setIndeterminate(true);
pb1.setVisibility(View.VISIBLE);
}
isBigPromotionLoadingTimeout = false;
} // END, onPreExecute
#Override
protected LatestUpdateResult doInBackground(Void... arg0) {
FunctionUtil.logD(DEBUGTAG, getClass().getSimpleName()+" doInBackground");
LatestUpdateResult response = null;
if (!isCancelled()) {
response = Util.getLatestUpdateWS(SplashActivity.this);
if (response!=null && StatusCode.WS_SUCCESSFUL==response.getErrorCode()) {
try {
//load the DB data of Promotion
long st = System.currentTimeMillis();
boolean b = Util.callGetPromotionLinkWS(SplashActivity.this, dbh);
long et = System.currentTimeMillis();
FunctionUtil.logD(DEBUGTAG, getClass().getSimpleName()+" doInBackground : Util.callGetPromotionLinkWS : "+b+" , load time="+(et-st)+"ms");
if (b) {
preLoadBigBanner();
int sleepTime = 500;
int totalSleepTime = 0;
while(!isFinishLoadingBigPromotion && totalSleepTime < Constant.BIGPROMOTIONWAITTIME){
FunctionUtil.logD(DEBUGTAG, "SplashActivity wait isFinishLoadingBigPromotion: "+isFinishLoadingBigPromotion);
try {
FunctionUtil.logD(DEBUGTAG, "SplashActivity wait for bigpromotion loading time : "+totalSleepTime);
totalSleepTime = totalSleepTime+sleepTime;
SystemClock.sleep(sleepTime);
} catch(Exception ex){
totalSleepTime = Constant.BIGPROMOTIONWAITTIME;
FunctionUtil.logE(DEBUGTAG, "doInBackground loadingBigPromotion error:", ex);
break;
}
} // END, while
isBigPromotionLoadingTimeout = true;
} // END, if
} catch(Exception e) {
FunctionUtil.logE(DEBUGTAG, getClass().getSimpleName()+" doInBackground error:", e);
}
} // END, if
} // END, isCancelled
return response;
} // END, doInBackground
#Override
protected void onPostExecute(LatestUpdateResult result) {
FunctionUtil.logD(DEBUGTAG, getClass().getSimpleName() + " onPostExecute > result="+result);
if(pb1 != null) {
pb1.setVisibility(View.GONE);
}
if (result!=null) {
int statusCode = result.getErrorCode();
if (statusCode==StatusCode.WS_SUCCESSFUL) {
Intent i1 = null;
/***
**/
goNext();
}
}
I have trouble using thread.join in my code below. It should wait for the thread to finish before executing the codes after it, right? It was behaving differently on different occasions.
I have three cases to check if my code goes well
App is used for the first time - works as expected but the loading page don't appear while downloading
App is used the second time (db is up to date) - works okay
App is used the third time (db is outdated, must update) - won't update, screen blacks out, then crashes
I think I have problems with this code on onCreate method:
dropOldSchedule();
dropThread.join();
triggerDownload();
Based on the logs, the code works until before this part... What can be the problem?
MainActivity.java
public class MainActivity extends Activity {
final static int INDEX_ACCTTYPE = 0;
final static int INDEX_ECN = 1;
final static int INDEX_TLN = 2;
final static int INDEX_SIN = 3;
final static int INDEX_MOBILE = 4;
final static int INDEX_CITY = 5;
final static int INDEX_START_DATE = 6;
final static int INDEX_START_TIME = 7;
final static int INDEX_END_DATE = 8;
final static int INDEX_END_TIME = 9;
final static int INDEX_REASON = 10;
final static int INDEX_DETAILS = 11;
DatabaseHandler db;
String str;
ProgressDialog pd;
TextView homeText1, homeText2, homeText3, homeText4;
final private String csvFile = "http://www.meralco.com.ph/pdf/pms/pms_test.csv";
final private String uploadDateFile = "http://www.meralco.com.ph/pdf/pms/UploadDate_test.txt";
Thread dropThread = new Thread(new Runnable() {
public void run() {
db = new DatabaseHandler(MainActivity.this);
db.dropOldSchedule();
runOnUiThread(new Runnable() {
public void run() {
while (!pd.isShowing());
db.close();
pd.dismiss();
}
});
}
});
Thread getUploadDateThread = new Thread(new Runnable() {
public void run() {
try {
URL myURL = new URL(uploadDateFile);
BufferedReader so = new BufferedReader(new InputStreamReader(myURL.openStream()));
while (true) {
String output = so.readLine();
if (output != null) {
str = output;
}
else {
break;
}
}
so.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
while (!pd.isShowing());
pd.dismiss();
}
});
}
});
Thread downloadThread = new Thread(new Runnable() {
public void run() {
db = new DatabaseHandler(MainActivity.this);
db.beginTransaction();
try {
URL url = new URL(csvFile);
Log.i("dl", "start");
InputStream input = url.openStream();
CSVReader reader = new CSVReader(new InputStreamReader(input));
Log.i("dl", "after reading");
String [] sched;
while ((sched = reader.readNext()) != null) {
if(sched[INDEX_CITY].equals("")) sched[INDEX_CITY]="OTHERS";
try {
db.addRow(sched[INDEX_SIN], sched[INDEX_CITY],
sched[INDEX_START_DATE], sched[INDEX_START_TIME],
sched[INDEX_END_DATE], sched[INDEX_END_TIME],
sched[INDEX_DETAILS], sched[INDEX_REASON]);
} catch (IndexOutOfBoundsException e) {
db.addRow(sched[INDEX_SIN], sched[INDEX_CITY],
sched[INDEX_START_DATE], sched[INDEX_START_TIME],
sched[INDEX_END_DATE], sched[INDEX_END_TIME],
"", sched[INDEX_REASON]);
//e.printStackTrace();
}
}
input.close();
Log.i("dl", "finished");
} catch (MalformedURLException e) {
e.printStackTrace();
db.endTransaction();
} catch (IOException e) {
e.printStackTrace();
db.endTransaction();
}
Log.d("Count", ""+db.count());
db.setTransactionSuccessful();
db.endTransaction();
writeUploadDateInTextFile();
}
});
#SuppressWarnings("unqualified-field-access")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pms_main);
Button home = (Button) findViewById(R.id.home);
home.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MeralcoSuite_TabletActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
homeText1 = (TextView) findViewById(R.id.home_text1);
homeText2 = (TextView) findViewById(R.id.home_text2);
homeText3 = (TextView) findViewById(R.id.home_text3);
homeText4 = (TextView) findViewById(R.id.home_text4);
homeText1.setVisibility(View.INVISIBLE);
homeText2.setVisibility(View.INVISIBLE);
homeText3.setVisibility(View.INVISIBLE);
homeText4.setVisibility(View.INVISIBLE);
getUploadDate();
try {
getUploadDateThread.join(); //wait for upload date
Log.d("getUploadDate","thread died, upload date=" + str);
if(dbExists()){
db = new DatabaseHandler(MainActivity.this);
Log.d("Count", "" + db.count());
db.close();
if(!uploadDateEqualsDateInFile()){
dropOldSchedule();
dropThread.join();
triggerDownload();
}
showDisclaimer();
Log.i("oncreate", "finished!");
return;
}
triggerDownload();
showDisclaimer();
Log.i("oncreate", "finished!");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void dropOldSchedule(){
if(pd!=null && pd.isShowing())
pd.setTitle("Getting upload date...");
else
pd = ProgressDialog.show(this, "Getting upload date",
"This may take a few minutes...", true, false);
dropThread.start();
}
public void triggerDownload() {
if (!checkInternet()) {
showAlert("An internet connection is required to perform an update, please check that you are connected to the internet");
return;
}
if(pd!=null && pd.isShowing())
pd.setTitle("Getting upload date...");
else
pd = ProgressDialog.show(this, "Getting upload date",
"This may take a few minutes...", true, false);
downloadThread.start();
}
public void getUploadDate() {
Log.d("getUploadDate", "getting upload date of schedule");
if(pd!=null && pd.isShowing())
pd.setTitle("Getting upload date...");
else
pd = ProgressDialog.show(this, "Getting upload date",
"This may take a few minutes...", true, false);
getUploadDateThread.start();
}
public void writeUploadDateInTextFile() {
Log.d("writeUploadDateTextFile", "writing:"+str);
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
"update.txt", 0));
out.write(str);
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
public void showDisclaimer() {
Log.d("ShowDisclaimer", "showing disclaimer");
homeText3
.setText("..." + str
+ "...");
homeText1.setVisibility(View.VISIBLE);
homeText2.setVisibility(View.VISIBLE);
homeText3.setVisibility(View.VISIBLE);
homeText4.setVisibility(View.VISIBLE);
Log.d("showDisclaimer", "finished showing disclaimer");
}
public boolean uploadDateEqualsDateInFile() {
Log.d("uploadDateEqualsDateInFile","comparing schedule upload dates");
try {
String recordedDate = "";
InputStream instream = openFileInput("update.txt");
if (instream != null) { // if file the available for reading
Log.d("uploadDateEqualsDateInFile","update.txt found!");
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line = null;
while ((line = buffreader.readLine()) != null) {
recordedDate = line;
Log.d("uploadDateEqualsDateInFile","recorded:"+recordedDate);
}
Log.d("uploadDateEqualsDateInFile","last upload date: " + str + ", recorded:" +recordedDate);
if(str.equals(recordedDate)) return true;
return false;
}
Log.d("uploadDateEqualsDateInFile","update.txt is null!");
return false;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean checkInternet() {
ConnectivityManager cm = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo infos[] = cm.getAllNetworkInfo();
for (NetworkInfo info : infos)
if (info.getState() == NetworkInfo.State.CONNECTED
|| info.getState() == NetworkInfo.State.CONNECTING) {
return true;
}
return false;
}
public boolean dbExists() {
File database=getApplicationContext().getDatabasePath(DatabaseHandler.DATABASE_NAME);
if (!database.exists()) {
Log.i("Database", "Not Found");
return false;
}
Log.i("Database", "Found");
return true;
}
#Override
protected void onDestroy() {
super.onDestroy();
if (db != null) {
db.close();
}
}
#Override
protected void onPause() {
super.onPause();
if (db != null) {
db.close();
}
}
}
Sorry but I couldn't find mistakes or problems in your code. But I would strongly recommend you to use AsyncTask for doing something in different thread. AsyncTask is very easy to use and I would say that it is one of the biggest advantages of java. I really miss it in obj-c.
http://labs.makemachine.net/2010/05/android-asynctask-example/
http://marakana.com/s/video_tutorial_android_application_development_asynctask_preferences_and_options_menu,257/index.html
check those links hope that will help you.
It was already mentioned that AsyncTask is the better alternative. However, it may be the case, that your call to join will throw InterruptedException. Try to use it like this:
while(getUploadDateThread.isRunning()){
try{
getUploadDateThread.join();
} catch (InterruptedException ie){}
}
// code after join
I think the problem that your facing is that you are blocking the UI thread when you call join in the onCreate() method. You should move this code into another thread which should execute in the background and once its done you can update the UI.
Here is a sample code:
final Thread t1 = new Thread();
final Thread t2 = new Thread();
t1.start();
t2.start();
new Thread(new Runnable() {
#Override
public void run() {
// Perform all your thread joins here.
try {
t1.join();
t2.join();
} catch (Exception e) {
// TODO: handle exception
}
// This thread wont move forward unless all your threads
// mentioned above are executed or timed out.
// ------ Update UI using this method
runOnUiThread(new Runnable() {
#Override
public void run() {
// Update UI code goes here
}
});
}
}).start();