简体   繁体   中英

How to make app refresh for new data everytime app is launch?

I am having hard time understanding, how Android app fetches data or refresh data everytime I launch app.

I have data it is update every minute meaning it is changed, but I don't see it update everytime I launch the app, only randomly it updates the data.

I tired, still doesn't work.

OnResume();

Here is my code.

package com.zv.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ZipActivity extends Activity {

    TextView textMsg, textPrompt;
    final String textSource = "https://docs.google.com/spreadsheet/pub?key=0AqSBI1OogE84dDJyN0tyNHJENkNyYUgyczVLX0RMY3c&single=true&gid=0&range=a2%3Aa26&output=txt";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textPrompt = (TextView)findViewById(R.id.textprompt);
        textMsg = (TextView)findViewById(R.id.textmsg);

        textPrompt.setText("Wait...");

        URL CPE;
        try {
            CPE = new URL(textSource);
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(CPE.openStream()));
            String StringBuffer;
            String stringText = "";
            while ((StringBuffer = bufferReader.readLine()) != null) {
                stringText += StringBuffer + "\n"; ;
            }
            bufferReader.close();
            textMsg.setText(stringText);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        }
        textPrompt.setText("Finished!");        
    }

}

You can override the Application class in your application. Define this in your manifest file. Every time your app starts -- no matter how it starts, user click, broadcast intent, service -- your Application class will get an on create.

I think you need to understand the life cycle of an application. Applications on android do not die because you close them. If you want it to update every time it is foregrounded you will have to override the onstart() method in your activities.

Everytime you start your activity the onCreate method is executed, this means that all the code inside that method will be executed again. If the content that you are fetching is not update is probably because is not updated from the source. But then again the refresh will only happen when you start your activity.

you can check this answer it might help you: https://stackoverflow.com/a/11456974/1552551

The Activity is the building block (better, it was , since now the Fragment API represent the base component) of an Android application. It has so-called lifecycle events which you can hook into. You named:

  • onCreate() , called by the framework when the Activity is first created. Possibly, this activity object can stay forever , even when other apps take the foreground and after two weeks the user touch you app's icon in the launcher: it may still be the very same Activity object. In reality, Activites are not so long lived, however you get the idea

  • onResume() , called after onPause() , which in turn is called when the Activity loses the foreground (Note that this has nothing to do with the activity's content visibility). Because onResume() is always called after onPause() , you are guaranteed that it will be called every time the user were doing something else and then switches to your activity, which seems just your use-case.

Note the difference: the user installs your app, touches the icon in the launcher, this fires an intent, the system detects that there's no activity around and creates a new instance. Then calls onResume() and finally takes the activity to the foreground. Next, your receive a SMS notification, you switch to the messanger, then come back to your old activity and onResume() gets called again (but not onCreate ).

So if you want to use a lifecycle hook to refresh the Activity's content, onResume() is what you are looking for. However note that once the URL content is read, the content may change and the client won't be notified if the activity still is in the foreground. So you may want to use some sort of server events (push) like C2DM (or websocket, or a plain socket, or whatever you are comfortable with).

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