Ich erstelle einen Thread für animierte Änderung Bild auf UI geplant. Es wird unterbrochen, während auf die Schaltfläche geklickt wird, und ein festes Bild festgelegt. Aber wenn setImage in runOnUiThread versehentlich nach setImage in onClick passieren, wird es das falsche Bild sein.Wie kann ich sicherstellen, dass irgendwo nach dem Thread unterbrochen wird?
Gibt es eine Möglichkeit in diesem Code kann sicher gehen, irgendwo nach der Rückkehr von InterruptedException. (Ich möchte keine Verzögerung auf ui Thread)
Bitte helfen Sie, danke!
thread = new Thread(){
@Override
public void run() {
while(!this.isInterrupted()){
for(int i=0; i<imageList.size(); i++){
if(getActivity()==null) return;
getActivity().runOnUiThread(new Runnable() {
public void run() {
ImageViewAnimatedChange(getActivity().getApplication(),
imgFunction, imageList.get(j), 0);
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
return;
}
}
}
}
};
// einmal onClick
if(thread!=null) {
thread.interrupt(); // catch interruptException
thread = null;
imgFunction.setImageResource(R.drawable.selector);
}
Edit: ich diesen Teil neu schreiben, wie vorgeschlagen.
public class asyncTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
Log.d(TAG, "doInBackground");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Log.w(TAG, e.toString());
return null;
}
final List<Bitmap> imageList = getImageList();
if(imageList.isEmpty()) return null;
Log.d(TAG, "start while ");
while(!isCancelled()){
Log.d(TAG, "while:" +Boolean.toString(isCancelled()));
for(int i=0; i<imageList.size()+1; i++){
final int j=i;
Log.d(TAG, "for" + Integer.toString(j));
if (j == imageList.size())
publishProgress(“ok”);
else
publishProgress(Integer.toString(i));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Log.w(TAG, e.toString());
return null;
}
}
}
Log.d(TAG, "while end");
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
QUtil.logE(TAG, "onPostExecute");
imgFunction.setImageResource(R.drawable.selector);
}
@Override
protected void onProgressUpdate(String... value) {
super.onProgressUpdate(value);
Log.d(TAG, "onProgressUpdate" + value[0]);
if(getActivity()==null || isCancelled()) return;
if(value[0]==“ok”)
ImageViewAnimatedChange(getActivity().getApplication(),
imgFunction, null, R.drawable.selector);
else
ImageViewAnimatedChange(getActivity().getApplication(),
imgFunction, getImageList().get(Integer.parseInt(value[0])), 0);
}
@Override
protected void onCancelled() {
super.onCancelled();
Log.e(TAG, "onCancelled");
try {
Thread.sleep(60);
} catch (InterruptedException e) {
Log.w(TAG, e.toString());
}
imgFunction.setImageResource(R.drawable.selector);
}
}
// beginnen
iftttAsyncTask = new IftttAsyncTask().execute("");
// end onClick
if(asyncTask!=null) {
asyncTask.cancel(true); // catch interruptException
threadIfttt = null;
}
manchmal manchmal nicht arbeiten dann arbeiten, nicht arbeiten, während "doInBackground" nicht in Schlaf Lauffläche ist! Es hat in onCancel gehen aber nicht wirklich abbrechen, ich mit isCanceled falsch bekam() Logs Bilder wie
hmmm .. Art des gleichen Problems mit AsyncTask, und was ich brauche in AsyncTask onProgressUpdate nicht onPostExecute, wird es den while-Thread beenden. Das Problem passiert aus void ImageViewAnimatedChange, das ich gemacht habe, benutze anim_in und anim_out darin, also passiert es immer noch, nachdem ich Image (ohne anim) gesetzt habe –