简体   繁体   中英

How to update xml list in android every minute

I am currently developing a android app which is generating a list. This list is generated from a php site which creates an xml file. this goes all very well and i am getting the results. But when the php site is updated the results in my android app ofcourse are not updated. can someone explain me how i let my android app automatically update the list say for every minute.

i've looked into alarm managers but i am not succesfull in refreshing my list automatically.

please help...

this is the code i have right now to generate the list:


package com.pxr.tutorial.xmltest;

import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends ListActivity {

    private ServiceCount count;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        count = new ServiceCount((long) (1 * 60 * 1000), 1000); // 1 minute
        count.start();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        String xml = XMLfunctions.getXML();
        Document doc = XMLfunctions.XMLfromString(xml);
        int numResults = XMLfunctions.numResults(doc);

        if ((numResults <= 0)) {
            Toast.makeText(Main.this, "Geen resultaten gevonden",
                    Toast.LENGTH_LONG).show();
            finish();
        }

        NodeList nodes = doc.getElementsByTagName("result");

        for (int i = 0; i < nodes.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nodes.item(i);
            map.put("id", XMLfunctions.getValue(e, "id"));
            map.put("beschrijving", XMLfunctions.getValue(e, "beschrijving"));
            map.put("type",  XMLfunctions.getValue(e, "type"));
            map.put("datum", XMLfunctions.getValue(e, "datum"));
            mylist.add(map);
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
                new String[] { "beschrijving", "type", "datum" }, new int[] {
                        R.id.item_title, R.id.item_subtitle,
                        R.id.item_subtitle1 });

        setListAdapter(adapter);
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv
                        .getItemAtPosition(position);
                Toast.makeText(Main.this,
                        "ID '" + o.get("id") + "' was clicked.",
                        Toast.LENGTH_LONG).show();
            }
        });
    }

    private class ServiceCount extends CountDownTimer
    {
        public ServiceCount(long millisInFuture, long countDownInterval)
        {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish()
        {
            count = new ServiceCount((long) (1 * 60 * 1000), 1000); count.start(); //start timer another time
            mylist.clear();
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onTick(long millisUntilFinished)
        {
            Log.d("timer", "" + millisUntilFinished / 1000);
        }
    }
}

You can set a Timer: http://developer.android.com/reference/java/util/Timer.html and set it again when the work is done. Also I'd use a service for this. BUT:

  • This won't work when the device is in standby because network is down.
  • If you force the device to wakeup it will kill the battery.

Are you sure you need to repeat the connection every minute?

I bet it'd be okay to refresh the data every 5-10 minutes if you enable the user to update data manually, just like google mail for example.

You shouldn't poll your server every minute. There's a solution to such a problem: GCM .

If you implement it on server, it'll tell all devices with your app, that data is updated and you can poll your server then. Or, if there isn't much data to download, even push your list with the GCM.

For this purpose I used Timer and Service .

public class ServiceCount extends CountDownTimer
{
    public ServiceCount(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish()
    {
        // update data
    }

    @Override
    public void onTick(long millisUntilFinished)
    {
        Log.d("timer", "" + millisUntilFinished / 1000);
    }
}

ServiceCount count = new ServiceCount((long) (1 * 60 * 1000), 1000); // 1 minute
count.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM