I think the easy way to resolve it is create a service which always are
running and notifying to the activity, this service should run a thread to
check periodically the data from the web service.
public class ViewSlideService extends Service {
public static final String TAG = "ViewSlideService";
Updater updater;
private final int DELAY = 3000;
private boolean isRunning = false;
Messenger outMessenger;
@Override
public void onCreate() {
super.onCreate();
updater = new Updater();
Log.v(TAG, "onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.v(TAG, "onStart");
licenseSeat = QwizdomCommon.licenseSeat;
isRunning = true;
updater.start();
}
@Override
public void onDestroy() {
super.onDestroy();
isRunning = false;
updater.interrupt();
updater = null;
// QwizdomWS.releaseSessionSeat(QwizdomCommon.licenseSeat.getSeatKey()); //
May be to finish we must release the session seat.
Log.v(TAG, "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
Log.v(TAG, "onBind");
Bundle extras = intent.getExtras();
if (extras != null) {
outMessenger = (Messenger) extras.get("MESSENGER");
}
return null;
}
private class Updater extends Thread {
@Override
public void run() {
super.run();
while (isRunning) {
UpdateResult update;
try {
//Get data from web service
Thread.sleep(DELAY);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
}
you can see this link for more info about how create a service:
http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging
El miércoles, 21 de noviembre de 2012 10:40:03 UTC-8, Antonis Kanaris
escribió:
>
> Hello.I am new developer android and i want to make an application read
> data from my webserver and control my arduino board...
> I start with this example .....for get data...ok work but only when click
> button....i want to convert to automatic update.How i make this?.
>
> public class ReadWebpageAsyncTask extends Activity {
> private TextView textView;
>
>
> /** Called when the activity is first created. */
>
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_read_webpage_async_task);
> textView = (TextView) findViewById(R.id.textView1);
> }
>
> private class DownloadWebPageTask extends AsyncTask<String, Void,
> String> {
> @Override
> protected String doInBackground(String... urls) {
> String response = "";
> for (String url : urls) {
> DefaultHttpClient client = new DefaultHttpClient();
> HttpGet httpGet = new HttpGet(url);
> try {
> HttpResponse execute = client.execute(httpGet);
> InputStream content = execute.getEntity().getContent();
>
> BufferedReader buffer = new BufferedReader(new
> InputStreamReader(content));
> String s = "";
> while ((s = buffer.readLine()) != null) {
> response += s;
> }
>
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> return response;
> }
>
> @Override
> protected void onPostExecute(String result) {
> textView.setText(result);
> }
> }
>
> public void readWebpage(View view) {
> DownloadWebPageTask task = new DownloadWebPageTask();
> task.execute(new String[] { "http://www.mysite.net/LEDstate.txt" });
>
> }
> }
>
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en