oncreate(){
setUpDemo();
}
public void setupDemo(){
imageDemo=new ImageView(this);
RelativeLayout.LayoutParams rLayParamsDirectiveBtnImg = new RelativeLayout.LayoutParams(height/9,height/9);
rLayout= (RelativeLayout) findViewById(R.id.relative_layout);
rLayout.setBackgroundColor(Color.MAGENTA);
demoObjectObject = dbHandler.getDemoObjectObject(trainingID);
final RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(height/3,height/3);
//putting middle to show demo of items in each iteration
rLayParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
handler = new Handler();
int i=0;
while ( i <demoObjectObject.size() ) {
// Log.d(TAG, "for i="+ i);
speech = "";
final int finalI = i;
handler.postDelayed(new Runnable() {
@Override
public void run() {
ObjectObject demoObject = new ObjectObject();
demoObject = demoObjectObject.get(finalI);
speech = KEY_THIS;
speech = speech + KEY_READ[1]+ dbHandler.getColorName(demoObject.getColorID());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);
}
else{
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
imgBytes = demoObject.getObjectImageBlob();
bmp = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
rLayout.removeView(imageDemo);
imageDemo.setImageBitmap(bmp);
rLayout.addView(imageDemo,rLayParams);
}
}, 5000 * i);
i++;
}//while end
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak("now it is starting...", TextToSpeech.QUEUE_FLUSH, null, null);
}
else{
tts.speak("now startnig...", TextToSpeech.QUEUE_FLUSH, null);
}
}
}, 5000 * demoObjectObject.size());
handler.postDelayed(new Runnable() {
@Override
public void run() {
setupForGame();
}
}, 5000 * demoObjectObject.size()+6000);
}
如你所见,我有一个处理程序。在里面,队列中有3个runnable。第一个是在一个while循环中。在每次迭代时,处理程序等待5秒钟。
当我停止活动(例如回家或桌面)时,我想通过停止tts来冻结这个等待时间。当我回来时,它应该从头开始说话,也许它应该从等待的第一秒开始(例如我在3秒后离开,但它可以从0秒开始)。
我试试那些
@Override
protected void onResume() {
super.onResume();
if (handler != null) {
synchronized (handler) {//this doesnot work.
//speak again
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);
}
else{
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
@Override
protected void onStop() {
Log.v(TAG, "choosing gameonstop");
super.onStop();
handler.removeCallbacksAndMessages();//this needs paramter but i dont have to put inside?
}
@Override
protected void onPause() {
super.onPause();
}
答案 0 :(得分:0)
public class YourActivity extends AppCompatActivity {
private static boolean handlerflag=false;
private Handler handler;
private Runnable runnable;
private int myind=0,index=0,count=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activtiy);
//oncreate exe only
handlerflag=true;
handler = new Handler();
startyourtime(0);
}
private void startyourtime(int a) {
myind=0;
for (index=a; index<10 ;index++) {
myind++;
runnable=new Runnable() {
count++;
@Override
public void run() {
//your code here
}
};handler.postDelayed(runnable, Constants.TIME_LIMIT * myind);
}
@Override
protected void onPause() {
super.onPause();
handlerflag=false;
handler.removeCallbacksAndMessages(null);
}
@Override
protected void onResume() {
super.onResume();
if(!handlerflag)
{
startyourtime(count);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}