我的处理程序不工作 smooth.How 我可以修复它吗?
My handlers don't work smooth.How can i fix it?
大家好,我尝试从 txt 文件中获取命令,我想 运行 这些命令以便在一段时间内 time.I 可以正确地从 txt 文件中获取命令但是当我想在一段时间内使用处理程序执行这些命令,但是有一个 problem.The 问题是处理程序一起工作,另一个处理程序在第一个处理程序的时间之前发挥作用 expires.When 我用相同的 运行让它做同样的事情。
我的 txt 文件如下所示:
伊莱里,5,1 /n
Sol,10,1 /n -->10 是运行时间 1 是重复次数。
凹陷,5,1 /n
Geri,10,1 /n
我的输出:
I/System.out: 伊莱里
6000
1个
I/System.输出:太阳
11000
1个
I/System.out:下垂
6000
1个
I/System.out: 杰里
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');
}
}
});
我使用 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();
}
}
});
大家好,我尝试从 txt 文件中获取命令,我想 运行 这些命令以便在一段时间内 time.I 可以正确地从 txt 文件中获取命令但是当我想在一段时间内使用处理程序执行这些命令,但是有一个 problem.The 问题是处理程序一起工作,另一个处理程序在第一个处理程序的时间之前发挥作用 expires.When 我用相同的 运行让它做同样的事情。
我的 txt 文件如下所示: 伊莱里,5,1 /n Sol,10,1 /n -->10 是运行时间 1 是重复次数。 凹陷,5,1 /n Geri,10,1 /n
我的输出:
I/System.out: 伊莱里 6000 1个 I/System.输出:太阳 11000 1个 I/System.out:下垂 6000 1个 I/System.out: 杰里 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');
}
}
});
我使用 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();
}
}
});