简体   繁体   English

离线时如何在Android应用程序中保存数据?

[英]How can I save data when offline in my Android app?

I'm creating an app for Android in Java. 我正在用Java创建适用于Android的应用程序。 I can send info to a webservice but when i'm offline i want to store the info and when the phone gets connected to the internet i want to send it to the webservice. 我可以将信息发送到Web服务,但是当我离线时,我想存储信息,而当电话连接到互联网时,我想将其发送到Web服务。 Can anyone help me? 谁能帮我? Thanks 谢谢

  1. Store your data in SQLite data-base. 将数据存储在SQLite数据库中。 You can read here . 您可以在这里阅读。

  2. Check in BroadcastReceiver if connectivity changed and update online DB and delete the content from Sqlite DB. 检查BroadcastReceiver中的连接性是否已更改,并更新在线DB并从Sqlite DB中删除内容。

     public void onReceive(Context context, Intent intent) { String s = intent.getAction(); if (s.equals("android.net.conn.CONNECTIVITY_CHANGE")) { if (IsNetworkAvailable(context)) { // update your online data-base and delete all rows from SQlite } return; } } 

Method isNetworkAvailable: 方法isNetworkAvailable:

public static boolean isNetworkAvailable(Context mContext) {
    Context context = mContext.getApplicationContext();
    ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED){
                    return true;
                }
             }
        }
    }
   return false;
}

You also need permissions: 您还需要权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Save your info to a SQLite database and use a BroadcastReceiver to listen when network connection is available. 将您的信息保存到SQLite数据库中,并在网络连接可用时使用BroadcastReceiver进行侦听。 When connection is available, start a Service that sends the saved data to your webservice. 当连接可用时,启动将已保存的数据发送到您的Web Service

In the event, where you upload your data, check if Internet connection is available or not, if not, store the data as textfile or object and name it like needToUpload1, 2, 3. 如果您在其中上传数据,请检查Internet连接是否可用,如果不可用,则将数据存储为文本文件或对象,并将其命名为needToUpload1、2、3。

Then, whenever the device is online, check if the there is any file named like that. 然后,每当设备在线时,检查是否有任何类似的文件名。 If so, upload it via service and delete the file. 如果是这样,请通过服务上传并删除文件。

That's just my opinion how i'll do it but really depends on data. 这只是我的意见,但实际上取决于数据。 You can find how to do every step on this site so i hope it will help. 您可以在该站点上找到如何执行每个步骤的方法,希望对您有所帮助。

Offline storage and sync seem simple at first sight but you need to be careful and consider the following: 脱机存储和同步乍看之下似乎很简单,但是您需要小心并考虑以下几点:

First get familiar with the storage options available in the platform and chose the most convenient for your app scenario. 首先,熟悉平台中可用的存储选项 ,然后选择最适合您的应用场景的存储。

Second, design your sync mechanism in an efficient way so your app won't drain the battery life. 其次,以一种有效的方式设计您的同步机制,以使您的应用不会耗尽电池寿命。

This is a good resource to check the entire structure of an android app that cares about working offline and properly sync with the server as network becomes available. 是检查android应用程序整体结构的好资源,该结构关心脱机工作并在网络可用时与服务器正确同步。

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

相关问题 如何将我的应用程序数据保存在我的android应用程序中? - How should i save my app data in my android app? 如何在我的 Android 应用程序中保存 HashMap? - How can I can save HashMap in my Android app? 如何在我的 android 应用程序中保存夜间模式的状态? - How can I save the state of the night mode in my android app? 如何保存数据persistend,以便我可以离线使用它们 - How to save data persistend so I can use them offline 当我的应用程序离线时,如何使spring.xml的xsi:schemaLocation工作(成品是离线工件) - How can I make spring.xml's xsi:schemaLocation work when my app is offline (finished product is an offline artifact) 如何在我的android应用中访问并显示离线英语词典? - How can I get access to and display an offline english dictionary in my android app? Android设备离线时如何存储数据? - How can data be stored in Android device while app is offline? 如何为Android应用对哈希图中的数据进行排序? - How can I sort data in a hashmap for my android app? 我何时应该在Android应用程序中读取/保存文件中的数据? - When should I read/save data in a file in an Android app? 我如何在我的 android 应用程序中保存打开的关卡 - How i save the opened levels in my android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM