我正在开发一个带有 Android 线程的简单应用程序。我的应用程序中有以下代码。但这似乎不起作用。
l = (RelativeLayout)findViewById(R.id.layout_main);
p = new LayoutParams(100, 100);
i = new ImageView(getApplicationContext());
i.setImageResource(R.drawable.bb);
i.setLayoutParams(p);
i.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (running) {
running = false;
} else {
new Thread(new Runnable() {
@Override
public void run() {
while (running) {
try {
Log.d("INFO", "Thread executing");
Thread.sleep(100);
} catch (Exception e) {
running = false;
}
}
}
}).start();
}
return false;
}
});
l.addView(i);
甚至日志也没有打印。
编辑:running = false;
在代码之前