gpt4 book ai didi

java - 如何*不*使用 runOnUiThread

转载 作者:行者123 更新时间:2023-12-01 15:13:56 28 4
gpt4 key购买 nike

正如您在标题中看到的,我试图不在每次需要时使用 runOnUiThread。稍后我会解释我的意思,但首先,这是我当前的代码(部分):

private void filePaste()
{
final File src = new File(FileClip); //Source file (as a string)
final File dest = new File(myPath.getText()+"/"+src.getName()); //Destination file
final ProgressDialog dlg = new ProgressDialog(AppContext);

final Thread copythread = new Thread(new Runnable() {
public void run() {
if(FileClip==null)
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_EmptyClip));
}
});
else
{
if(src.canRead()){
if(!src.isDirectory())
{
try{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(src), 8192);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));

long pos = 0;
long read = 8192;
byte[] data = new byte[(int)read];

while(pos<src.length()){
int bytes = in.read(data, 0, (int)read);
if(bytes>-1) {
out.write(data,0,bytes);
in.skip(read);
in.mark((int)pos+bytes);
in.reset();
dlg.incrementProgressBy(bytes);
pos+=bytes;
}
}

Main.this.runOnUiThread(new Runnable() {
public void run(){
dlg.dismiss();
}
});
in.close();
out.close();
}catch(final Exception e)
{
Main.this.runOnUiThread(new Runnable() {
public void run(){
alert("filePaste():\n"+e);
}
});
}

if(Moving==true) {
boolean q = src.delete();

if(q==true)
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_MoveOK,src.getName()));
}
});
else
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_MoveNO,src.getName()),1);
}
});

Moving = false;
}
else {
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_CopyOK,src.getName()));
}
});
}
}
}
else
Main.this.runOnUiThread(new Runnable() {
public void run(){
toast(getString(R.string.msg_CopyNO,src.getName()));
}
});
FileClip = null;
Main.this.runOnUiThread(new Runnable() {
public void run(){
getDir(myPath.getText().toString());
}
});
}
}
});

dlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dlg.setTitle(Moving?getString(R.string.moving):getString(R.string.copying));
dlg.setMessage(src.getName());
dlg.setCancelable(true);
dlg.setButton(DialogInterface.BUTTON_NEGATIVE, getString(android.R.string.cancel), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
dlg.cancel();
}
});
dlg.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog) {
copythread.interrupt();
if(dest.exists())
dest.delete();
}
});
dlg.setMax((int)src.length());
dlg.show();

copythread.start();
}

此代码的作用是尝试复制给定文件(作为字符串存储在 FileClip 中)。

可以看到,代码相当长,因为过度使用了runOnUiThread。我想知道如何移动所有

Main.this.runOnUiThread(new Thread(new Runnable()
public void run()
{
//Simple stuff for 6 lines
}
));

去上课什么的。我在想

public class runAtUI implements Runnable

但我在那一刻就停下来了,我不知道如何创建构造函数。我希望你明白我的意思;像这样:

new runAtUI()
{
//This makes less lines IMO, and could be executed
}

*注意:我查找了与此相关的主题,但都说相同,使用 runOnUiThread。我知道这没问题,但对于一些简短的事情(比如显示一个 toast - 使用 toast() 具有相同的目的 - 或再次发出警报,但使用alert() - )这是毫无意义的。

PS:这是我正在做的一个简单的文件管理器,由于缺乏资金,不会在 Google Play 上发布,而且我也不想使用广告(破坏了名称中的“简单性”) )。是的,我知道有免费、无广告(这个词存在吗?)和更好的应用程序,但我想学习如何制作一个 n_n;

任何信息、想法或指南将不胜感激。

<小时/>

编辑#2:感谢所有快速回复的人!您提供的示例目前可以使用,但我希望它更加灵活。就像 javascript 中的 eval("code as string") (代码不是字符串)。我会考虑回答这个问题,但将其保留以让人们提供更多想法;D

<小时/>

编辑#3:好的,首先,很抱歉很长时间没有回复。其次,我添加了指向当前代码的链接,从 123 行(这一行)到 pastebin 处的 77 行代码。 。我还添加了 uithread 的代码。关于该应用程序:如果您想要对其进行测试,看看它现在怎么样或其他什么,请给我发邮件,我会将其发送给您;)

最佳答案

您可以创建一个实用程序类来封装创建由 UI 线程运行的可运行对象并调用 Toast 的步骤。这是一个简单的实现:

public abstract class ToastUtils {
/**
* Displays a toast message, making sure that the toast is always invoked from the main (ui) thread.
* @param act Calling Activity
* @param text Toast text
* @param duration Toast duration
*/
public static void showToast(final Activity act, final String text, final int duration) {
act.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(act, text, duration).show();
}
});
}
}

使用此类,您可以在必要时执行此操作:

ToastUtils.showToast(this, "一些文本", Toast.LENGTH_SHORT);

关于java - 如何*不*使用 runOnUiThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11912176/

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