Activity的AsyncTask请求

来源:互联网 发布:手机淘宝话费怎么退款 编辑:程序博客网 时间:2024/05/16 05:11

工具类ActivityUtils代码:

import android.app.ProgressDialog;import android.content.Context;import android.os.AsyncTask;import java.util.concurrent.Callable;public class ActivityUtils {    private static ProgressDialog dialog;    public static <T> void doAsync(final Context context, final String title, final String message,final Callable<T> callable,final Callback<T> callback){        AsyncTask<Void,Void,T> task=new AsyncTask<Void, Void, T>() {            @Override            protected void onPreExecute() {                super.onPreExecute();                if(context!=null && message!=null && !"".equals(message)){                    dialog=ProgressDialog.show(context,title,message);                }            }            @Override            protected T doInBackground(Void... params) {                try {                    return callable.call();                } catch (Exception e) {                    e.printStackTrace();                    if(dialog!=null){                        dialog.dismiss();                    }                }                return null;            }            @Override            protected void onPostExecute(T result) {                super.onPostExecute(result);                if(dialog!=null){                    dialog.dismiss();                }                callback.onCallback(result);            }        };        task.execute();    }}

调用方式:

ActivityUtils.doAsync(mContext, "", "进行中...", new Callable<String>() {@Overridepublic String call() throws Exception {//将执行代码的返回值回调回去return Httpget.validproblem(jobno,time,scanStano,scanStationo,"11","22",createOperator);}}, new Callback<String>() {@Overridepublic void onCallback(String myResult) {//结果处理}});
回调接口函数:

public interface Callback<T> {    public void onCallback(final T result);}




0 0
原创粉丝点击