开发者

Refresh data Android application

开发者 https://www.devze.com 2023-04-09 16:28 出处:网络
I made an android application that brings information using a web service, then when the application starts I put the data in a sqlite and I manage everything from my database.After the application st

I made an android application that brings information using a web service, then when the application starts I put the data in a sqlite and I manage everything from my database. After the application starts the data fills a tablelayout.

Now I want to REFRESH that content every 20 seconds (Because information from webservice could changed).

How can I do this? I used the onResume method, but I don't want to refresh content every time you went back to the tablelayout.

So what I want to do is to execute the oncreate method 开发者_高级运维(which connect with a webservice, fills my tablelayout and display it) every 20 seconds. I read about timer or handler but I'm not sure how can I do this.

Now i have a problem!

i get data from web service and insert data in database in my doInBackground.. thats ok. Now, i create all textviews, the tablerows, etc in the onPostExecute method, but i have 2 problems. First,

UsuariosSQLiteHelper usdbh = new UsuariosSQLiteHelper(this, "DBIncidentes", null, 1);

I have a context problem there, in the doInBackground method

and in onPostExecute method, i have the same problem with all the "this" , like TableRow rowTitulo = new TableRow(this);

i know this is a context error, i know basically how context works, but i dont know how to resolve this context problem. i thought that initializing a context in the async constructor may help and i replace in the onpost.. please help!


First off you do not want to be reading data from a web service OR writing it to a SQLite database in your onCreate method. You need to spawn a new thread for doing this so that it does not cause your application to freeze up. You could create a Thread or use an AsyncTask for this. If you use an AsyncTask, then you can override its onPostExecute method. This will be executed on the main UI thread, so you can refresh your TableLayout in this method.

Once you've got this code working properly, then you just need to schedule it. The easiest way to do this is using a Timer. Here is some skeleton code to help you get started:

class MyTask extends AsyncTask<Void,Void, MyDataStructure>{
    @Override
    protected MyDataStructure doInBackground(Void... params) {
        MyDataStructure data = new MyDataStructure();
        // get data from web service
        // insert data in database
        return data;
    }

    @Override
    protected void onPostExecute(MyDataStructure data) {
        TableLayout table = (TableLayout) findViewById(R.id.out);
        // refresh UI
    }
}

Timer timer = new Timer();
TimerTask task = new TimerTask(){

    @Override
    public void run() {
        new MyTask().execute();
    }

};
long whenToStart = 20*1000L; // 20 seconds
long howOften = 20*1000L; // 20 seconds
timer.scheduleAtFixedRate(task, whenToStart, howOften);


Maybe you could start a new thread that runs an infinite loop that fetches the data every 20 seconds:

    private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                fetchData();
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    t.start();
}

private void fetchData() {
    // Get the data from the service

    mHandler.post(new Runnable() {
        @Override
        public void run() {
            // This will run on the ui thread
            // Update UI with the data here
        }
    });
}

If you need the to refresh the data even while your activity is stopped, you should look into services: http://developer.android.com/guide/topics/fundamentals/services.html


A timer task and a thread should work. Here is an example:

Timer refreshTask = new Timer();
Thread refreshThread = new Thread(new Runnable() {
    public void run() {
      //pull data
    }
  });

refreshTask.scheduleAtFixedRate(new TimerTask(){ 
    public void run()
    {
        onTimerTick_TimerThread();
    }

}, 0, 30000L);

public void onTimerTick_TimerThread()
{

    refreshThread.start();
}

Please see this link as it has a better example of how to handle threading.

0

精彩评论

暂无评论...
验证码 换一张
取 消