Android - 使用GCM更新客户端的sqlite数据库 [英] Android - update client' sqlite database using GCM

查看:195
本文介绍了Android - 使用GCM更新客户端的sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQLite数据库开发一个android应用程序。我希望实现以下功能:当有新数据可用时,客户端应用程序会自动将新数据下载到其sqlite数据库中。
$ b






我读了很多相关帖子,许多人建议GCM是通过创建GCM服务器向客户端发送通知来执行此功能的最佳方法'设备。例如,本教程然而,它只是显示GCM服务器在客户端应用程序上发送的消息。 GCM服务器应该发送什么信息以便从服务器更新客户端的sqlite数据库? (发送一个sql update命令?)

感谢高级!
$ b $

======== ================================================== ==



编辑:




GCM服务器通知客户端有一个新的更新,然后客户端调用Web服务来获取数据并插入到它的sqlite数据库。

问题
:编写这种Web服务的方法是什么?如何将客户端应用程序的数据库与我的服务器同步?

它只是为了贴心你的应用程序,哟!我的服务器上有一些新的更新,让我们连接并下载新的数据。它可以节省大量的设备电池,您可以在内部检测更新可用性时将您的服务器集中在一起。



您可以指定一个键 - 您从GCM发送给您的应用的json消息的值,如
{
status:newupdate
} $ /

在GCMReceiver的onMessage()中,您可以通过意图和写入逻辑(例如

  if(message.equalsIgnoreCase(newupdate){

//调用一个服务并下载数据并在你的数据库中存储/更新


如果您是Sqlite数据库的新用户,请参阅本教程
http://www.vogella.com/articles/AndroidSQLite/article.html



更新数据库考虑到你已经实现了他的OpenHelper类

  MyOpenHe lper myOpenHelper = new MyOpenHelper(this)
sqltitedatabaseObject = myOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(< your_column_name>,< value>);

//用于更新现有条目
long rowAffected = sqltitedatabaseObject.updateWithOnConflict(< Table_Name> ;,
值,< Selection_For_Update>,< Selection_Paramans_In_String_Array>,
0);
//用于插入新行
long rowsInserted = sqltitedatabaseObject.insert(< Table_Name>,null,values);

按照您的编辑让我添加几点:


  1. 同步意味着保持两个数据相同,为了达到这个目的,你必须创建一个web服务来获取数据库中的数据 - 插入数据库中的每个数据的标记并将其与发送到设备的数据一起发送)。同步数据从设备sqlite数据库中获取时间戳的最大值,并在从设备点击Web服务时作为参数发送,并发送数据在服务器的响应中,在请求中收到的特定时间戳后添加或更新。


    因此,即使用户删除了你的应用程序,你将在请求时间内获取任何东西,并发送整个数据。


    I am working on an android app with SQLite database. I want to implement the following feature:

    • when there is new data available, clients' app will download the new data to its sqlite database automatically.

    I read a lot of relate posts, many people recommended that GCM is the best approach to do such function by creating a GCM server to send notification to clients' device. For example, this tutorial.

    However, it just display the message sent by GCM server on client's app. What should the GCM server send in order to update the client's sqlite database from server? (send a sql update command?)

    Thanks in advanced!

    ============================================================

    EDIT:

    what I know at this stage: GCM server notify the clients there is a new update, then the client call a web service to fetch data and insert into its sqlite datbase.

    Problems: what is the approach of writing such web service? how to sync the client app's database with my server one?

    解决方案

    GCM server is not responsible to update you database, you can use it just to intimate your app that, yo! there is something new on my server to update, lets connect and download new data. It saves a lot of battery of device which you would be wasting in pooling your server on some defined time internal for checking the availability of update.

    You can specify a key-value for json message which you are sending to your app from GCM like
    { "status" : "newupdate" }

    and in onMessage() of GCMReceiver you can get the message by key "status" from intent and write logic like

    if(message.equalsIgnoreCase("newupdate"){
    
     //Call a service and download the data and store/update in your databse 
    
    }
    

    Go for this tutorial if you are new for Sqlite Database http://www.vogella.com/articles/AndroidSQLite/article.html

    To Update the database considering you already implemented he OpenHelper class

     MyOpenHelper myOpenHelper= new MyOpenHelper(this)
     sqltitedatabaseObject=myOpenHelper.getWritableDatabase();
     ContentValues values = new ContentValues();
     values.put(<your_column_name>,<value>);
    
    // For Updating the Existing Entry
    long rowAffected=sqltitedatabaseObject.updateWithOnConflict(<Table_Name>,
                values,<Selection_For_Update>,<Selection_Paramans_In_String_Array>,
                0);
    // For Inserting a New Row
            long rowsInserted=sqltitedatabaseObject.insert(<Table_Name>, null, values);
    

    As per your Edit let me add few points :

    1. Sync means keeping both data same, And to achieve this you have to create a webservice which will fetch the data from your database (add a time-stamp for each data inserted in database and send it along with the data you are sending to device ).

    2. While syncing data fetch the max value of time stamp from the device sqlite database and send as a parameter while hitting the web-service from device, and send data in response from server which are added or updated after that particular time-stamp received in request.

    So even if the user deleted you app, you will get nothing in request time stampso send whole data.

    这篇关于Android - 使用GCM更新客户端的sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆