我试图多次调用我的函数,其中包含runnable对象。 谁能告诉我我做错了什么?
handler = new Handler();
sButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
rounds = new Integer(et.getText().toString());
// handler.removeCallbacksAndMessages(null);
for (int j = 0; j <= rounds; j ++)
{
f();
}
}
});
}
public void f()
{
r = new Runnable()
{
@Override
public void run()
{
mp = MediaPlayer.create(Box.this, R.drawable.sound1);
mp.setLooping(false);
mp.setVolume(1.0f, 1.0f);
mp.start();
handler.postDelayed(this, 3000);
}
};
handler.postDelayed(r, 4000);
mp.stop();
提前致谢
答案 0 :(得分:1)
看看这个 -
Handler handler;
sButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
rounds = new Integer(et.getText().toString());
for (int j = 0; j <= rounds; j ++)
{
f();
}
}
});
public void f()
{
r = new Runnable()
{
@Override
public void run()
{
handler = new Handler() {
public void handleMessage(Message msg) {
mp = MediaPlayer.create(Box.this, R.drawable.sound1);
mp.setLooping(false);
mp.setVolume(1.0f, 1.0f);
mp.start();
handler.sendEmptyMessageDelayed(1,3000);//3 sec
msg.what = 2;//removing first executed handler this is u missing.
};
};
handler.sendEmptyMessageDelayed(1,4000);//4sec
}
}
}