在Android中运行一段时间后执行异步任务

时间:2017-01-26 09:07:19

标签: java android android-asynctask

我正在开发一个应用程序,我需要通过Web服务更新我的活动文本框。它运作良好。我想在执行后检查来自Web服务的更新。所以我在asynctask调用方法中创建了一个计时器。我的异步任务类是单独的类。但我的异步任务无效..没有错误。请帮我解决这个问题。

public class MainActivity extends Activity {
private View chart;
private Button btnChart;

Context context;
Activity activity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   callAsynchronousTask();
}

      //Calling AsynchronoustTask
  public void callAsynchronousTask() {
        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            ProductSummary ps = new ProductSummary(context,activity);
                            ps.new GetSummary().execute();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 40000);
    }

产品摘要类

 public class ProductSummary {
    private static String url = "http://localhost/contacts.asmx";

private Context context;
Activity activity;
ProgressDialog pDialog;

public ProductSummary(Context context,Activity activity){

    this.context = context;
    this.activity=activity;
}
public class GetSummary extends AsyncTask<String, String,HashMap<String, String>> {
    //Other codes were written here.
    }

0 个答案:

没有答案