【问题标题】:Doing 2 things in background with asynctask使用 asynctask 在后台做两件事
【发布时间】:2014-08-05 13:27:22
【问题描述】:

我想创建启动画面。一方面,我通过 webservice dans 从服务器检索信息,另一方面,我的初始屏幕有一个进度轮 (https://github.com/Todd-Davies/ProgressWheel)。

问题是我的轮子在我的 Web 服务执行异步任务期间被阻塞。

我尝试在轮子的异步任务上执行 OnExecutor(),但它不起作用。

轮子有我的异步任务:

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

        @Override
        protected Void doInBackground(Void... params) {
            pw = (ProgressWheel) findViewById(R.id.pw_spinner);
            pw.spin();
            return null;
        }
    }

我的 Webservice 异步任务是一个经典任务(从数据库获取信息,解析为 JSON 并发送到应用程序。它工作正常。 我使用 task.get() 来获取我的信息。我听说这个函数强制代码等待 Asynctask 结束才能继续。可能是这个吗?

我只想在我的 WebService 检索信息期间让我的 progressWheel 旋转。

我该怎么做?

编辑: 我可以有 2 个不同的异步任务吗?一个用于我的网络操作,另一个用于旋转?可能吗 ?

【问题讨论】:

  • 你必须反其道而行之.. 联系 doInBackground 中的 web 服务并在 onCreate() 中初始化 ProgressWheel
  • 是的,你可以有两个异步任务
  • 你能举个例子吗?我试过了,但它不起作用。我的轮子在我的 WebService 处于活动状态时被阻止:/
  • 我已经编辑了我的代码并从互联网上下载了图片,所以它可以正常工作

标签: java android json web-services android-asynctask


【解决方案1】:

你的异步任务是错误的。您正在后台线程中访问 UI 视图:

private class LogoTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        pw = (ProgressWheel) findViewById(R.id.pw_spinner);
        pw.spin();//Ui elements should run in ui thread
    }
    @Override
    protected Void doInBackground(Void... params) {
       //network operations here
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        //stop progress wheel or dismiss
    }
}

更新根据您的要求,我尝试了该库。这是对我有用的代码:

public class CustomProgressBar extends Dialog {
Context context;
String message;
TextView textViewMessage;
ProgressWheel wheel;

public CustomProgressBar(Context context, String message) {
    super(context);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.message = message;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    setContentView(R.layout.dialog_progress);
    textViewMessage = (TextView) findViewById(R.id.textViewMessage);
    wheel = (ProgressWheel) findViewById(R.id.progressWheel);

}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    wheel.spin();
    textViewMessage.setText(message);
}

}

对应的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/fragment_background"
android:padding="15dp" >

<TextView
    android:id="@+id/textViewMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/progressWheel"
    android:ellipsize="end"
    android:paddingLeft="10dp"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/black" />

<com.todddavies.components.progressbar.ProgressWheel
    android:id="@+id/progressWheel"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    ProgressWheel:barColor="#0097D6"
    ProgressWheel:barLength="40dp"
    ProgressWheel:barWidth="5dp"
    ProgressWheel:rimColor="@color/rim_body"
    ProgressWheel:rimWidth="1dp"
    ProgressWheel:spinSpeed="3dp" />

实例化类:

CustomProgressBar bar = new CustomProgressBar(yourContext,"Loading");
//onpreexecute
bar.show();
//doInBackground()
do network operations
//onPostExecute()
bar.dismiss()

我让它看起来像一个没有标题栏的进度对话框。

【讨论】:

    【解决方案2】:

                pw = (ProgressWheel) findViewById(R.id.pw_spinner);
                pw.spin();
    

    在 PreExecute 方法中

                protected void onPreExecute() {
                    super.onPreExecute();
                    pw = (ProgressWheel) findViewById(R.id.pw_spinner);
                    pw.spin();
                }
    

    并在 onPostExecute 方法中关闭它

       protected void onPostExecute(String abc) {
           // dismiss it
            }
    

    【讨论】:

    • 它不起作用。我的轮子在 web 服务期间被阻塞:(
    【解决方案3】:

    实际上,我使用的是 get() 方法!并且 get() 等待 AsyncTask 结束以返回。所以我的轮子在异步任务运行期间被阻塞了。

    我找到了 BroadCast Intent 的解决方案。 我在 PostExecute 上发送广播,并且我的班级中有一个调用 WebService 的接收器。它工作得很好! :)

    【讨论】:

      猜你喜欢
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      相关资源
      最近更新 更多