大家好,我尝试从txt文件中获取命令,并且我想在一定时间段内运行这些命令。我可以从txt文件中正确获取命令,但是当我想使用处理程序来执行这些命令时一定的时间,但是有一个问题。问题在于处理程序可以一起工作,另一个处理程序在第一个处理程序的时间到期之前就开始起作用了。当我使用相同的可运行程序执行此操作时,它执行的操作也相同。
我的txt文件如下所示: Ileri,5,1 / n Sol,10,1 / n-> 10是操作时间1是重复次数。 凹陷,5,1 / n Geri,10,1 / n
我的输出:
I / System.out:Ileri 6000 1个 I / System.out:Sol 11000 1个 I / System.out:下垂 6000 1个 I / System.out:Geri 11000 1个 I / System.out:A
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer=new Timer();
String yon;
String saniye1;
String tekrar;
int i=0,j=0,c=0,d=0;
ArrayList<String> listS=new ArrayList<String>();
try {
Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));
while(s.hasNextLine())
{
listS.add(s.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] line2=new String[100];
for(String str:listS)
{
String[] line=str.split(",");
line2[c]=line[i];
line2[c+1]=line[i+1];
line2[c+2]=line[i+2];
c=c+3;
}
//String gonderilecek=new String();
String [] line3=new String[c+2];
while(d<c)
{
line3[d]=line2[d];
d=d+1;
}
d=0;
tekrar=line3[2];
int tekrar1=Integer.parseInt(tekrar);
while(d<tekrar1)
{
while(j<=c-2)
{
yon = line3[j];
saniye1 = line3[j + 1];
sure1 = Long.parseLong(saniye1);
sure1=sure1*1000;
sure1=sure1+1000;
System.out.println(yon);
System.out.println(sure1);
System.out.println(tekrar1);
e=0;
switch (yon)
{
case "Ileri":
runnable=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp.start();
}
else
{
handler.removeCallbacks(runnable);
}
}
};
handler.postDelayed(runnable,1000);
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;
case "Sol":
runnable2=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp2.start();
}
else
{
handler.removeCallbacks(runnable2);
}
}
};
handler.postDelayed(runnable2,1000);
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;
case "Sag":
runnable3=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp3.start();
}
else
{
handler.removeCallbacks(runnable3);
}
}
};
handler.postDelayed(runnable3,1000);
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;
case "Geri":
runnable4=new Runnable() {
@Override
public void run() {
sure1=sure1-1000;
if(sure1>0)
{
handler.postDelayed(this,1000);
mp4.start();
}
else
{
handler.removeCallbacks(runnable4);
}
}
};
handler.postDelayed(runnable4,1000);
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
break;
}
j=j+3;
}
j=0;
d=d+1;
System.out.println('A');
}
}
});
答案 0 :(得分:0)
我通过使用thread.sleep()解决了我的问题;我在这里写是因为有人可能需要我的代码。
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer=new Timer();
///data/data/com.example.emrecan.myapplication/files/komutlar.txt
String yon;
String saniye1;
String tekrar;
int i=0,j=0,c=0,d=0;
ArrayList<String> listS=new ArrayList<String>();
try {
Scanner s=new Scanner(new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt"));
while(s.hasNextLine())
{
listS.add(s.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] line2=new String[100];
for(String str:listS)
{
String[] line=str.split(",");
line2[c]=line[i];
line2[c+1]=line[i+1];
line2[c+2]=line[i+2];
c=c+3;
}
String [] line3=new String[c+2];
while(d<c)
{
line3[d]=line2[d];
d=d+1;
}
d=0;
tekrar=line3[2];
int tekrar1=Integer.parseInt(tekrar);
while(d<tekrar1)
{
while(j<=c-2) {
yon = line3[j];
saniye1 = line3[j + 1];
sure1 = Long.parseLong(saniye1);
sure1 = sure1 * 1000;
//sure1 = sure1 + 1000;
System.out.println(yon);
System.out.println(sure1);
System.out.println(tekrar1);
e = 0;
while (sure1>0)
{
switch (yon) {
case "Ileri":
try {
outputStream.write(1);
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
case "Sol":
try {
outputStream.write(2);
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
case "Sag":
try {
outputStream.write(3);
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
case "Geri":
mp4.start();
try {
outputStream.write(4);
} catch (IOException e1) {
e1.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
try {
Thread.sleep(1000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
break;
default:
Toast.makeText(getApplicationContext(), "Sıkıntı var!", Toast.LENGTH_LONG).show();
break;
}
sure1=sure1-1000;
}
j=j+3;
}
j=0;
d=d+1;
System.out.println('A');
}
try {
outputStream.write(10);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});