我有一个异步任务,我想每10秒重复一次,以便将焦点移回到Android应用程序的用户界面中。
我尝试使用java.util.Timer和TimerTask,但应用程序崩溃。如您所见,它在for循环中有效,但我需要每10秒重复一次。 这样做...即使我包含Thread.Sleep(10000),延迟,while循环也不起作用。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.os.AsyncTask;
public class MainActivity extends AppCompatActivity {
TextView statusDisplay,display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=(TextView)findViewById(R.id.display);
display.setText("display");
statusDisplay=(TextView)findViewById(R.id.statusDisplay);
statusDisplay.setText("status");
//I need to make this call every 10 secs
new MyAsyncTask().execute("start");//Works fine as a single execution
}
//Our AsynTask
// Specify your own types <params,progress,result>
private class MyAsyncTask extends AsyncTask<String,Integer,Integer>
{
String status="Task being setup";
int counter=0;
//Step 1 that is executed for setting up our Asynchronous task
@Override
protected void onPreExecute()
{
super.onPreExecute();
display.setText(status);
statusDisplay.setText(String.valueOf(counter));
}
//Step 2 runs in the background and is only executed once
//So put whatever computations including access to the Network inside this method
//Do not make any calls to the UI from inside this method as its running in the background
@Override
protected Integer doInBackground(String[] params)
{
try{
status=params[0];
for(int i = 0;i<10;i++){
counter++;
status = "Task Running" + " " + String.valueOf(counter) + " " + "of" + " " + "10";
publishProgress(counter);//Calls onProgressUpdate
Thread.sleep(1000);
}
}
catch(InterruptedException ex){
}
status = "Task completed";
return counter;
}
//Step 3 called when we make a call to publishProgress in doInBackground
@Override
protected void onProgressUpdate(Integer[] values)
{
super.onProgressUpdate(values);
display.setText(String.valueOf(values[0]));
statusDisplay.setText(status);
}
//Step 4 Called after completion of doInBackground. Its return value is passed on to this method
//Make final update changes to your UI at this step
@Override
protected void onPostExecute(Integer result)
{
super.onPostExecute(result);
display.setText(String.valueOf(result));
statusDisplay.setText(status);
}
}
}
答案 0 :(得分:1)
您可以使用ScheduledExecutorService
:
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable yourTaskRunner = new Runnable() {
public void run() { new MyAsyncTask().execute("start"); }
}
scheduler.scheduleAtFixedRate(yourTaskRunner, 0, 10, TimeUnits.SECONDS);
described here的四种方式之一。
答案 1 :(得分:0)
这是香港专业教育学院的尝试,但没有看到延迟。它只会定期重新运行
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.os.AsyncTask;
import android.os.Handler;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
TextView statusDisplay,display;
int x=0;
Handler handler = new Handler();
int stopcondition=0;
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=(TextView)findViewById(R.id.display);
display.setText("display");
statusDisplay=(TextView)findViewById(R.id.statusDisplay);
statusDisplay.setText("status");
//new MonitorSecurityStatus().execute("start");//This works fine
//handler.post(rerunCode);//A)Runs periodically and terminates when stop-condition occurs but we don't see the 5 sec delay
//handler.postDelayed(rerunCode,5000);//B) Executes rerunCode after 5 sec delay we don't need this
scheduler.scheduleWithFixedDelay(rerunCode,0,5000, TimeUnit.MILLISECONDS);//C) Behaves same as A)
}
private Runnable rerunCode = new Runnable() {
@Override
public void run()
{
//handler.postDelayed(this,5000);//A) We dont see the 5 secs delay behaves same as if this declaration was inside else-if
stopcondition++;
if(stopcondition==10){
handler.removeCallbacks(this);
}
else if(stopcondition<10)
{
new MonitorSecurityStatus().execute("start");
handler.postDelayed(this,5000);//A) We dont see the 5 secs delay
}
}
};