@Override
public void onPause() {
super.onPause();
}
private void generateView() {
for (int i = 0; i < 80; i++) {
ln_highlightprogress.setVisibility(View.VISIBLE);
LinearLayout mview = new LinearLayout(getActivity());
mview.setBackgroundResource(R.color.grayColor);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(7, 40);
layoutParams.setMargins(2, 0, 2, 0);
mview.setLayoutParams(layoutParams);
lnLinearlayout.addView(mview);
Log.d("==================", "=================" + i);
Log.v("==================", "=================" + i);
}
changeProgress();
}
private void changeProgress() {
mChangeProgressThread = new Thread(new Runnable() {
@Override
public void run() {
// Update the progress status
for (int i = 0; i < lnLinearlayout.getChildCount(); i++) {
final int finalI = i;
progressStatus = i;
// Try to sleep the thread for 20 milliseconds
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
handler.post(new Runnable() {
@Override
public void run() {
if (highlightButtonWorkComplete && finalI == secondYellowColoredTextViewPosition) {
} else {
lnLinearlayout.getChildAt(finalI).setBackgroundResource(R.color.white);
}
if (finalI == (lnLinearlayout.getChildCount() - 1)) {
for (int i = 0; i < lnLinearlayout.getChildCount(); i++) {
if (highlightButtonWorkComplete && finalI == secondYellowColoredTextViewPosition) {
// leave this textview , as it is yellow ... go to next textview
secondYellowColoredTextViewPosition = 0;
highlightButtonWorkComplete = false;
} else {
lnLinearlayout.getChildAt(i).setBackgroundResource(R.color.grayColor);
}
}
Log.d("Progress Status", "ProgressStatus" + progressStatus);
if (progressStatus % 79 == 0) {
lnLinearlayout.removeAllViews();
generateView();
}
}
}
});
}
}
});
mChangeProgressThread.start();// Start the operation
}
private void changeHighlight() {
secondYellowColoredTextViewPosition = progressStatus + endTime; //
highlightButtonWorkComplete = true;
lnLinearlayout.getChildAt(progressStatus - statartTime ).setLayoutParams(new LinearLayout.LayoutParams(7, 100));
lnLinearlayout.getChildAt(progressStatus - statartTime ).setBackgroundResource(R.color.yellow);
mHightlightThread = new Thread(new Runnable() {
@Override
public void run() {
final int size = endTime + (statartTime);
for (int j = 0; j <= size - 1; j++) {
lastlopp = j;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(new Runnable() {
@Override
public void run() {
lnLinearlayout.getChildAt(progressStatus - statartTime).setBackgroundResource(R.color.yellow);
if (lastlopp == size - 1) {
lnLinearlayout.getChildAt(secondYellowColoredTextViewPosition).setLayoutParams(new LinearLayout.LayoutParams(10, 100));
lnLinearlayout.getChildAt(secondYellowColoredTextViewPosition).setBackgroundResource(R.color.yellow);
}
}
});
}
//got?yes .. may be calculation problem
}
});
mHightlightThread.start();
}
答案 0 :(得分:0)
在线程中使用布尔标志。如果boolean为true,则不执行其他代码。在onPause方法中,您可以使用线程代码中将使用的布尔标志为false。希望有所帮助
你可以做什么做一个全局布尔变量并且在onPause上调用set boolean true let的布尔是stopThread
stopThread = true
在循环中首先检查
if(stopThread) {
break;
}
答案 1 :(得分:0)
而不是使用Thread
,Handler
和Thread.sleep
,请尝试仅使用Handler。并尝试在Handler中使用postDelayed
。并且您可以使用removeCallbacks
这里给出了使用Handler而不是Thread更改onProgressChange方法的示例。
Runnable MyRunnable = new Runnable() {
@Override
public void run() {
if (highlightButtonWorkComplete && finalI == secondYellowColoredTextViewPosition) {
} else {
lnLinearlayout.getChildAt(finalI).setBackgroundResource(R.color.white);
}
if (finalI == (lnLinearlayout.getChildCount() - 1)) {
for (int i = 0; i < lnLinearlayout.getChildCount(); i++) {
if (highlightButtonWorkComplete && finalI == secondYellowColoredTextViewPosition) {
// leave this textview , as it is yellow ... go to next textview
secondYellowColoredTextViewPosition = 0;
highlightButtonWorkComplete = false;
} else {
lnLinearlayout.getChildAt(i).setBackgroundResource(R.color.grayColor);
}
}
Log.d("Progress Status", "ProgressStatus" + progressStatus);
if (progressStatus % 79 == 0) {
lnLinearlayout.removeAllViews();
generateView();
}
}
if(progressStatus < 80) {
mHandler.postDelayed(this, 1000);
}
}
};
private int progressStatus = 0;
private Handler mHandler = new Handler();
@Override
protected void onResume() {
super.onResume();
generateView();
}
@Override
protected void onPause() {
super.onPause();
mHandler.removeCallbacks(MyRunnable);
}
private void generateView() {
for (int i = 0; i < 80; i++) {
ln_highlightprogress.setVisibility(View.VISIBLE);
LinearLayout mview = new LinearLayout(getActivity());
mview.setBackgroundResource(R.color.grayColor);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
7, 40);
layoutParams.setMargins(2, 0, 2, 0);
mview.setLayoutParams(layoutParams);
lnLinearlayout.addView(mview);
Log.d("==================", "=================" + i);
Log.v("==================", "=================" + i);
}
progressStatus = 0;
mHandler.postDelayed(MyRunnable, 1000);
// changeProgress();
}