gpt4 book ai didi

java - 单击时禁用线程

转载 作者:行者123 更新时间:2023-12-01 13:10:54 26 4
gpt4 key购买 nike

我正在开发计算特定应用程序运行时间的应用程序,为了节省内存,我想通过使用复选框来停止一些线程。我想要一个命令,当我按下复选框时,即使“如果条件”正确,线程也会停止”

检测应用的if条件:

if (MainActivity.tgbutton.isChecked())
{
if (packageName.equals("com.facebook.katana"))
{

if (f==1)
{

}
else
{
// Toast.makeText(getApplicationContext(),"fb on", Toast.LENGTH_SHORT).show();
f=1;
facebook = true;
startTimef = SystemClock.uptimeMillis();
customHandlerf.postDelayed(updateTimerThreadfacebook, 0);
}
}
else
{
if(f==1)
{
//Toast.makeText(getApplicationContext(),"fb off", Toast.LENGTH_SHORT).show();

facebook = false;
timeSwapBufff += timeInMillisecondsf;

customHandlerf.removeCallbacks(updateTimerThreadfacebook);
f=2;
}
}
}

复选框:

blabla.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if (((CheckBox) v).isChecked()) {

// what to write here to disable thread???
}

}
});

线程本身:

private Runnable updateTimerThreadfacebook = new Runnable() {


public void run() {


if (facebook = true)
{
timeInMillisecondsf = SystemClock.uptimeMillis() - startTimef;

updatedTimef = timeSwapBufff + timeInMillisecondsf;

int secsf = (int) (updatedTimef / 1000);
int minsf = secsf / 60;
secsf = secsf % 60;

MainActivity.facebook.setText("" + minsf + ":" + String.format("%02d", secsf));

customHandlerf.postDelayed(this, 0);

}
}

};

最佳答案

您应该在运行方法中使用值

public class MyClass implements Runnable{
boolean signal=false;
public setisFinish(boolean sig)
{
signal = sig;
}

@Override
public void run()
{
if(!signal)
//Do SomeThing
}

public void foo()
{
System.out.println("foo");
}

}

并在您的点击监听器中

blabla.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if (((CheckBox) v).isChecked()) {

myThread.setisFinish(true);
}

}

});

此代码恢复您的线程,您可以随时启动它,如果您想中断您的线程,请使用此代码

myThread.interrupt();

关于java - 单击时禁用线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22879721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com