简体   繁体   English

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

[英]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. 我很难理解,每次启动应用程序时Android应用程序如何获取数据或刷新数据。

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. 您可以在应用程序中覆盖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. 每当您的应用启动时-无论它如何启动,用户单击,广播意图,服务-您的Application类都将在创建时创建。

I think you need to understand the life cycle of an application. 我认为您需要了解应用程序的生命周期。 Applications on android do not die because you close them. android上的应用程序不会因为关闭它们而消失。 If you want it to update every time it is foregrounded you will have to override the onstart() method in your activities. 如果您希望它在每次前台更新时都进行更新,则必须在活动中重写onstart()方法。

Everytime you start your activity the onCreate method is executed, this means that all the code inside that method will be executed again. 每当您开始活动时,都会执行onCreate方法,这意味着该方法内的所有代码将再次执行。 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 您可以查看此答案,它可能会对您有所帮助: 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. Activity是Android应用程序的构建块(更好的 ,因为Fragment API代表了基本组件)。 It has so-called lifecycle events which you can hook into. 它具有可以挂接到的所谓生命周期事件 You named: 您命名:

  • onCreate() , called by the framework when the Activity is first created. onCreate() ,由框架在首次创建Activity时调用。 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. 即使其他应用程序占据了前台,并且两周后用户在启动器中触摸您应用程序的图标,该活动对象也可能永远存在 :它可能仍然是相同的Activity对象。 In reality, Activites are not so long lived, however you get the idea 实际上,Activites的寿命并不长,但是您可以理解

  • 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). onResume() ,在onPause()之后调用,当Activity失去前景时被调用(请注意,这与活动的内容可见性无关)。 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. 因为onResume()总是在onPause()之后调用,所以可以确保每次用户执行其他操作时都会调用它,然后切换到您的活动,这似乎只是您的用例。

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. 然后调用onResume() ,最后将活动带到前台。 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 ). 接下来,您会收到一条SMS通知,您切换到信息收发器,然后返回到以前的活动,并且再次调用onResume() (但不会调用onCreate )。

So if you want to use a lifecycle hook to refresh the Activity's content, onResume() is what you are looking for. 因此,如果您想使用生命周期挂钩来刷新Activity的内容,则需要onResume() 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. 但是请注意,一旦读取URL内容,则该内容可能会更改,并且如果活动仍在前台,则不会通知客户端。 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). 因此,您可能想使用某种服务器事件 (推送),例如C2DM (或websocket或普通套接字,或任何您喜欢的东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次从后台恢复应用程序时,如何刷新 Webview? - How do I refresh Webview everytime the app is brought back from background? Android Studio-首次启动应用程序时如何将数据加载到应用程序中? - Android Studio - How to load data into the app when the app first launch? 在应用启动时如何制作(并保存在RAM中)sparsearray? (安卓) - How to make (and save in RAM) sparsearray at app launch? (android) 每次应用程序打开Android时如何执行代码 - How to execute code everytime app opens Android 每次启动应用程序时如何执行OTP验证? - How to perform OTP verification everytime the app is launched? 每次更新数据时如何让客户端获取? - How to make a client fetch everytime data is updated? 无法使 Admob 应用打开广告出现在应用启动时 - Can't make Admob App Open Ads to appear on app launch 我用Java做过一个简单的游戏。 是否可以刷新控制台,以便每次接收新数据时仅显示该信息? - I made a simple game in Java. Is it possible to refresh the console, so that everytime it receives new data it displays only that? 新应用数据决策 - New App Data Decision Android如何使整个应用程序中的数据保持一致 - Android how to make data consistent in whole app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM