I want call the following portion of code every second??
I have tried the following-
But don't know how to call every second??
How can I keep on calling new checkNWConnectivity().execute("Test"); everysecond and from where to call in my android program???
private class checkNWConnectivity extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
ConnectivityManager manager = (ConnectivityManager)getSystemService(MainActivity.CONNECTIVITY_SERVICE);
// 3G confirm
Boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
// wifi confirm
Boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
if(is3g){
tvNWConnectivity.setText(" Connected to 3G");
}else if(isWifi){
tvNWConnectivity.setText(" Connected to wifi");
}else{
String text = " No Network Connectivity."+"\n" + " Uploading will be resumed from streamlet "+countStreamletUploaded.toString();
tvNWConnectivity.setText(text);
}
return null;
}
}
So my question is how to call it every second?
Actually I want to keep on running the portion of code in doInBackground(String... params) { ... }
You can use TimerTask, Timer and a Handler to do this:
public void toCallAsynchronous() {
TimerTask doAsynchronousTask;
final Handler handler = new Handler();
Timer timer = new Timer();
doAsynchronousTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
handler.post(new Runnable() {
public void run() {
try {
checkNWConnectivity performBackgroundTask = new checkNWConnectivity();
performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
timer.schedule(doAsynchronousTask, 0,30000); //put the time you want
}
You want to use a ScheduledExecutorService
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(command, initialDelay, period, unit);
Create the executor OnCreate of your activity, start the execution onResume and stop it onPause.
then you can call runOnUiThread(action);
to post actions back to the UI.
At first you can't work with views from non-UI thread so you can't call TextView.setText
from AsyncTask.doInBackground
method. You can simply use Handler and submit a message every second using Handler.sendMessageDelayed
the way I did this was to put the thread in a Service
the service would do a countdown timer from a very large number, so it would never get to zero, but that was just a use case for my app, you might need something that goes forever
then there is the dilemma of manipulating the UI thread. My loop in the Service would edit a variable in SharedPreferences and my Activity had a loop checked SharedPreferences and would update UI objects if SharedPreferences returned a certain value, problem solved.
You may be able to do something similar with broadcast receivers
Use Handler.
private class MyHandler extends Handler{
@Override
public void handleMessage(Message message){
YourAsyncTask task = new YourAsyncTask(...);
task.execute();
this.sendMessageDelayed(new Message(), 1000);}
}
Then when you want to start just do this
MyHandler handler = new MyHandler();
handler.sendMessageDelayed(new Message(), 1000);