用asynctask在后台做两件事

时间:2014-08-05 13:27:22

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

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

在我的Webservice在asynctask中执行期间,我的车轮被阻挡了。

我试图在我的asynctask for wheel上执行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的asynctask是一个经典的(从数据库获取信息,在JSON中解析并发送到应用程序。它工作正常。 我使用 task.get()来获取我的信息。我听说这个函数强制代码等待Asynctask的结束继续。可能是这个吗?

我只是希望在我的WebService检索信息期间我的progressWheel旋转。

我该怎么做?

编辑: 我可以有2个不同的asynctask吗?一个用于我的网络操作而另一个用于旋转?可能吗 ?

3 个答案:

答案 0 :(得分:4)

你的asynctask是错误的。您正在后台线程中访问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()

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

答案 1 :(得分:0)

            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
        }

答案 2 :(得分:0)

实际上,我使用的是get()方法!并且get()等待AsyncTask结束重新调用。所以我的车轮在asynctask运行期间被阻挡了。

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