Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我想在UI线程上运行AsyncTask-我怎么做?_Android - Fatal编程技术网

Android 我想在UI线程上运行AsyncTask-我怎么做?

Android 我想在UI线程上运行AsyncTask-我怎么做?,android,Android,我有一个特定的例子,我想在UI线程上运行AsyncTask的doInBackground。我该怎么做呢?据我所知。。。。。你不能 设计用于执行后台任务,需要 表演时间更长 因此,它们必须在不同于 主线 Mainthread负责应用程序的所有UI 如果要在执行后台操作时更新UI: 只需使用AsyncTask的onProgressUpdate方法更新 后台操作期间的UI 否则,在调用beckground之前,使用onPre方法更新UI 制造 否则最后使用onPost方法在rteasync t

我有一个特定的例子,我想在UI线程上运行AsyncTask的doInBackground。我该怎么做呢?

据我所知。。。。。你不能

  • 设计用于执行
    后台任务
    ,需要 表演时间更长
  • 因此,它们必须在不同于 主线
  • Mainthread
    负责应用程序的所有UI

如果要在执行后台操作时更新UI

  • 只需使用
    AsyncTask
    onProgressUpdate
    方法更新 后台操作期间的UI
  • 否则,在调用
    beckground
    之前,使用
    onPre
    方法更新UI 制造
  • 否则最后使用
    onPost
    方法在rte
    async task
    完成
    doInBackground

{SAMPLE}
私有类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
对于(int i=0;i<5;i++){
试一试{
睡眠(1000);
}捕捉(中断异常e){
Thread.interrupted();
}
}
返回“已执行”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
TextView txt=(TextView)findViewById(R.id.output);
txt.setText(“已执行”);//txt.setText(结果);
//可能需要更改传递的返回字符串的“已执行”
//进入onPostExecute(),但这取决于您
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}
据我所知。。。。。你不能

  • 设计用于执行
    后台任务
    ,需要 表演时间更长
  • 因此,它们必须在不同于 主线
  • Mainthread
    负责应用程序的所有UI

如果要在执行后台操作时更新UI

  • 只需使用
    AsyncTask
    onProgressUpdate
    方法更新 后台操作期间的UI
  • 否则,在调用
    beckground
    之前,使用
    onPre
    方法更新UI 制造
  • 否则最后使用
    onPost
    方法在rte
    async task
    完成
    doInBackground

{SAMPLE}
私有类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
对于(int i=0;i<5;i++){
试一试{
睡眠(1000);
}捕捉(中断异常e){
Thread.interrupted();
}
}
返回“已执行”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
TextView txt=(TextView)findViewById(R.id.output);
txt.setText(“已执行”);//txt.setText(结果);
//可能需要更改传递的返回字符串的“已执行”
//进入onPostExecute(),但这取决于您
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}

您当然可以,但正如@Devrath所说,这是毫无意义的。AsyncTask用于后台操作

下面是一个使用runOnUiThread方法的示例:

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // WORK on UI thread here
            }
        });
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//在这里处理UI线程
}
});
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}

您当然可以,但正如@Devrath所说,这是毫无意义的。AsyncTask用于后台操作

下面是一个使用runOnUiThread方法的示例:

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // WORK on UI thread here
            }
        });
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//在这里处理UI线程
}
});
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}

我最后做了以下几件事:

从doInBackground获取所有代码并将其移动到一个方法

在doInBackground中,我只调用这个方法

在需要正常执行asynctask的情况下,我调用LongOperation().execute()


在需要在UI线程上运行asynctask的情况下,我不调用它,而是调用该方法。

我最终执行了以下操作:

从doInBackground获取所有代码并将其移动到一个方法

在doInBackground中,我只调用这个方法

在需要正常执行asynctask的情况下,我调用LongOperation().execute()

在需要在UI线程上运行asynctask的情况下,我不调用它,而是调用该方法。

试试这个

public void commentBuilder() {
new Thread(new Runnable() {
@Override
public void run() {
//Your code 
}
}).start();
}
此外,请参见此了解更多详细信息