java - 如何在每次应用程序启动时刷新应用程序以获取新数据?

标签 java android

我很难理解 Android 应用程序如何在每次启动应用程序时获取数据或刷新数据。

我有数据每分钟更新一次,这意味着它会发生变化,但我每次启动应用程序时都看不到它更新,只是随机更新数据。

我累了,还是不行。

OnResume();

这是我的代码。

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!");        
    }

}

最佳答案

您可以覆盖应用程序中的 Application 类。在 list 文件中定义它。每次你的应用程序启动时——无论它如何启动、用户点击、广播 Intent 、服务——你的应用程序类都会得到一个创建。

我认为您需要了解应用程序的生命周期。 Android 上的应用程序不会因为关闭而消失。如果您希望它在每次前台更新时更新,则必须重写 Activity 中的 onstart() 方法。

关于java - 如何在每次应用程序启动时刷新应用程序以获取新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750927/

相关文章:

java - 更新 Firebase 中的键值

java - 如何找到有效子集和最佳有效子集?

java - Android - 如何使用 Intent.ACTION_CALL 激活调用紧急号码?

android - NineOldAndroids 中的 LayoutTransition 类..?

java - 云数据流: reading entire text files rather than lines by line

java - 将两个列表结构链接在一起

java - eclipse R.java 无法打开类文件

Android获取偏好复选框

android - phonegap-facebook-插件 : only works when native FB app is not installed (Android)

android - 数据库查询到ListActivity