如何在android中每x分钟运行一个异步任务? [英] How to run an async task for every x mins in android?

查看:11
本文介绍了如何在android中每x分钟运行一个异步任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在特定时间运行异步任务?(我想每 2 分钟运行一次)

how to run the async task at specific time? (I want to run it every 2 mins)

我尝试使用延迟发布但它不起作用?

I tried using post delayed but it's not working?

    tvData.postDelayed(new Runnable(){

    @Override
    public void run() {
        readWebpage();

    }}, 100);

在上面的代码中,readwebpage 是为我调用异步任务的函数..

In the above code readwebpage is function which calls the async task for me..

现在下面是我正在使用的方法

Right now below is the method which I am using

   public void onCreate(Bundle savedInstanceState) {

         readwebapage();

   }

   public void readWebpage() {
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute("http://www.google.com");

   }

   private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response1 = "";
        response1=read(); 
                   //read is my another function which does the real work    
        response1=read(); 
        super.onPostExecute(response1);
        return response1;
    }


      protected void onPostExecute(String result) {


         try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            TextView tvData = (TextView) findViewById(R.id.TextView01);
            tvData.setText(result);

        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.google.com" });

    }

    }

这就是我的代码,它工作得很好,但我耗尽了电池电量的大问题?

This is what I my code is and it works perfectly fine but the big problem I drains my battery?

推荐答案

如果你想每 X 秒启动一次,你可以使用 handler.处理程序很好,因为在触发事件时您不需要额外的线程来保持跟踪.这是一个简短的片段:

You can use handler if you want to initiate something every X seconds. Handler is good because you don't need extra thread to keep tracking when firing the event. Here is a short snippet:

private final static int INTERVAL = 1000 * 60 * 2; //2 minutes
Handler mHandler = new Handler();

Runnable mHandlerTask = new Runnable()
{
     @Override 
     public void run() {
          doSomething();
          mHandler.postDelayed(mHandlerTask, INTERVAL);
     }
};

void startRepeatingTask()
{
    mHandlerTask.run(); 
}

void stopRepeatingTask()
{
    mHandler.removeCallbacks(mHandlerTask);
}

请注意,doSomething 不应花费很长时间(例如 UI 中音频播放的更新位置).如果可能需要一些时间(例如下载或上传到网络),那么您应该使用 ScheduledExecutorServicescheduleWithFixedDelay 函数代替.

Note that doSomething should not take long (something like update position of audio playback in UI). If it can potentially take some time (like downloading or uploading to the web), then you should use ScheduledExecutorService's scheduleWithFixedDelay function instead.

这篇关于如何在android中每x分钟运行一个异步任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆