我想运行一个非常简单的4个进程,每个进程都有一个间隔时间我正在使用CountDownTimer
运行第一个进程,当它完成后运行第二个进程然后运行第二个进程,然后运行第三个进程直到最后一个,因为你可以做什么过程应该做的是让圆圈进度可见。
下面的代码适用于方法StartProcess(30000,1)
的第一次调用,如果我单独使用,但不起作用我想要运行该过程四次:
StartProcess(30000,1);//Run progress bar on imageButton1
StartProcess(40000,2);//Run progress bar on imageButton2
StartProcess(50000,3);//Run progress bar on imageButton3
StartProcess(60000,4);//Run progress bar on imageButton4
只有四个进程运行,我知道还有等待归档这个,到目前为止我能找到它,你能帮忙吗。
参见图片:https://drive.google.com/file/d/0Bzg0AQsGg3kkWGpEa3pGX3R6S0k/view?usp=sharing
MainActivity中的此代码
package com.example.WaterProcess;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ProgressBar progress1,progress2,progress3,progress4;
ImageView img_Backwash, img_BrineSlot,img_BrineRefill,img_FastRinse;
TextView txt_timer;
public static final String MyPREFERENCES = "MyPrefs";
public int nProcessID=0;
public boolean bProcessStop=false;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
img_Backwash=(ImageView)findViewById(R.id.img_backwash);
img_BrineSlot =(ImageView)findViewById(R.id.img_brine_slow);
img_BrineRefill=(ImageView)findViewById(R.id.img_brine_refill);
img_FastRinse=(ImageView)findViewById(R.id.img_fast_rinse);
progress1 =(ProgressBar) findViewById(R.id.loadingPanel1);
progress2 =(ProgressBar) findViewById(R.id.loadingPanel2);
progress3 =(ProgressBar) findViewById(R.id.loadingPanel3);
progress4 =(ProgressBar) findViewById(R.id.loadingPanel4);
progress1.setVisibility(View.INVISIBLE);
progress2.setVisibility(View.INVISIBLE);
progress3.setVisibility(View.INVISIBLE);
progress4.setVisibility(View.INVISIBLE);
txt_timer=(TextView)findViewById(R.id.txt_timer);
btn_Start=(Button) findViewById(R.id.bnt_start);
btn_Start.setOnClickListener(new View.OnClickListener() {
@Override[enter image description here][1]
public void onClick(View view) {
StartProcess(30000,1);//Run progress bar on imageButton1
StartProcess(40000,2);//Run progress bar on imageButton2
StartProcess(50000,3);//Run progress bar on imageButton3
StartProcess(60000,4);//Run progress bar on imageButton4
}
});
}
public void StartProcess(long Interval,int ProcessID) {
this.nProcessID=ProcessID;
// for (int nCount = 1; nCount<= 4; nCount++) {
// Log.d("Conteo en FOR", Integer.toString(nCount));
new CountDownTimer(Interval, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText(Long.toString(millisUntilFinished/1000));
UpdateProcess(nProcessID,true);//Los Progress
Log.d("Conteo en Segundos", Long.toString(millisUntilFinished));
}
public void onFinish() {
txt_timer.setText("Done");
UpdateProcess(nProcessID);
}
}.start();
//}
// Log.d("Conteo------->", "Fin del Conteo");
}
void UpdateProcess(int ProcessNumber){
progress4 =(ProgressBar) findViewById(R.id.loadingPanel4);
switch (ProcessNumber){
case 1:
img_Backwash.setImageResource(R.drawable.btn_complete);
progress1.setVisibility(View.GONE);
nProcessID=0;
break;
case 2:
img_BrineSlot.setImageResource(R.drawable.btn_complete);
progress2.setVisibility(View.GONE);
nProcessID=0;
break;
case 3:
img_BrineRefill.setImageResource(R.drawable.btn_complete);
progress3.setVisibility(View.GONE);
nProcessID=0;
break;
case 4:
img_FastRinse.setImageResource(R.drawable.btn_complete);
progress4.setVisibility(View.GONE);
nProcessID=0;
break;
}
}
void UpdateProcess(int ProcessNumber,boolean IsProgressBar){
switch (ProcessNumber){
case 1:
progress1.setVisibility(View.VISIBLE);
break;
case 2:
progress2.setVisibility(View.VISIBLE);
break;
case 3:
progress3.setVisibility(View.VISIBLE);
break;
case 4:
progress4.setVisibility(View.VISIBLE);
break;
}
}
}
答案 0 :(得分:0)
正如你在问题中所说的那样(我使用CountDownTimerto在完成后以指定的间隔运行第一个进程然后运行第二个进程,然后运行第三个进程直到最后一个进程)。如果我理解你想以顺序方式一个接一个地执行任务
但是您为每个请求使用了CountDownTimer类,因此所有请求都将同时启动。要在上一个任务完成后启动新任务,您需要在上一个任务的完成方法上调用下一个任务。以下是您可以使用的示例代码。
public void StartProcess(long Interval,int ProcessID) {
this.nProcessID=ProcessID;
new CountDownTimer(Interval, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText(Long.toString(millisUntilFinished/1000));
UpdateProcess(nProcessID,true);//Los Progress
Log.d("Conteo en Segundos", Long.toString(millisUntilFinished));
}
public void onFinish() {
txt_timer.setText("Done");
UpdateProcess(nProcessID);
if(null != listener) {
listener.onTaskComplete(nProcessID);
}
}
}.start();
//}
// Log.d("Conteo------->", "Fin del Conteo");
}
public static interface TaskCompletedListener {
public void onTaskComplete(int processId);
}
private class TaskListener implements TaskCompletedListener {
public void onTaskComplete(int processId) {
switch(processId) {
case 1:
break;
StartProcess(40000,2);
case 2:
StartProcess(50000,3)
break;
case 3:
StartProcess(60000,4);
break;
}
}
}
在你的onclick事件中你喜欢这个
btn_Start.setOnClickListener(new View.OnClickListener() {
@Override[enter image description here][1]
public void onClick(View view) {
StartProcess(30000,1);//Run progress bar on imageButton1
}
});
注意:listener对象是TaskListener的一个实例,您需要在实例化时在类级别创建声明。