Android : Blocking the main thread

on Saturday, December 13, 2014


I am not understanding why my main UI thread is getting blocked. I have button which calls function doSomething().



randomButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
doSomething();
}
});
}

// My private helper funtion

private void doSomething() {

Thread t1 = new Thread() {

@Override
public void run() {

long startTime = SystemClock.uptimeMillis();
Log.e(TAG, "Start Time = "+startTime);
while((SystemClock.uptimeMillis()-startTime)<6000){// Loop for 6 seconds
Log.e(TAG, "Diff ="+(SystemClock.uptimeMillis()-startTime));
}
}
};
t1.run();
}


The problem is when I press the button, it remains pressed for how much ever time my loop run (here 6 secs). I want it in a such a way that user can still use his UI elements even though the loop keeps running. Thats the reason I created a new Thread. Am I doing something wrong ?


Thanks


0 comments:

Post a Comment