[英]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
谢谢
Store your data in SQLite data-base. 将数据存储在SQLite数据库中。 You can read here .
您可以在这里阅读。
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.