以下是代码,如果单击按钮,则应该开始结果活动:
public class Tab19 extends Activity {
ImageButton button1;
SoundPool mSoundPool;
AssetManager assets;
int catSound;
int countLoadedSound;
Context mContext;
ProgressDialog dialog;
int count = 0;
TextView t;
boolean has_been_clicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab19);
count = getIntent().getIntExtra("CountNum", 0);
mContext = this;
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
assets = getAssets();
catSound = loadSound("catSound.mp3");
button1 = (ImageButton)findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
playSound(catSound);
Intent firstIntent = new Intent(Tab19.this, Results.class);
TextView t = (TextView)findViewById(R.id.t);
t.setText("score: " + ++count +"/18");
firstIntent.putExtra("CountNum", count);
has_been_clicked = true;
startActivity(firstIntent);
finish();
}
});
new Thread(
new Runnable() {
public void run() {
while (!has_been_clicked) {
try {
// Thread will sleep for 10 seconds
sleep(10*1000);
} catch (Exception e) {
}
}
Intent i=new Intent(getBaseContext(),Results.class);
i.putExtra("CountNum", count);
startActivity(i);
finish();
return;
}
private void sleep(int i) {
// TODO Auto-generated method stub
}
}
).start(); }
@Override
protected void onDestroy() {
super.onDestroy(); }
protected void playSound(int sound) {
if (sound > 0)
mSoundPool.play(sound, 1, 1, 1, 0, 1);
}
private int loadSound(String fileName) {
AssetFileDescriptor afd = null;
try {
afd = assets.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Не могу загрузить файл " + fileName,
Toast.LENGTH_SHORT).show();
return -1;
}
return mSoundPool.load(afd, 1);
} }
如果没有点击按钮,Thread应该在10秒内启动下一个Activity,但是没有发生,请帮我找错。提前致谢
答案 0 :(得分:0)
进入你的线程,将while循环更改为if语句:
if (!has_been_clicked) {
try {
// Thread will sleep for 10 seconds
sleep(10*1000);
} catch (Exception e) {
}
}
答案 1 :(得分:0)
我认为您应该将代码更改为
boolean has_been_clicked = true;
...
while (has_been_clicked) {
has_been_clicked = false;
try {
// Thread will sleep for 10 seconds
sleep(10*1000);
} catch (Exception e) {
}
}
Intent i=new Intent(getBaseContext(),Results.class);
i.putExtra("CountNum", count);
startActivity(i);
finish();
return;