如何将数据从服务器发送到Android? [英] How to send data from server to Android?

查看:148
本文介绍了如何将数据从服务器发送到Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我希望服务器将一些数据发送到我的应用程序(而无需从移动设备调用Web服务)。就像一个网络面板,该面板操作移动应用程序以添加数据。因此,当用户在网站中添加数据并单击添加时,应将数据添加到移动应用程序(如果移动设备已连接到互联网)。这也就像向Android应用发送命令一样。
我计划为此使用推送通知(GCM),但是推送通知将不是一个好选择,因为我不希望用户知道我们正在向移动应用添加数据。即使该应用程序未处于活动状态或未打开,它也应添加。

I am working on a project where I want my server to send some data to my application (without calling of web service from mobile). It is like a web panel which operates mobile app to add data. So when user add data in website and click add, it should add that data to the mobile app (if mobile is connected to the internet). It is also like sending command to android app. I planned to use Push Notification (GCM) for this, but push notifications will not be a good option as I dnt want user to know that we are adding data in mobile app. It should add even if the app is not in active state or opened.

我认为我有以下3种选择

I think I have below 3 options


  1. 在服务器和移动设备。开始客户端服务器通信

  1. Make session between server and mobile. Start client server communication

使用SMS发送命令,然后根据要求调用Web服务

Use SMS to send command then call web service as per the requirement

调用一次Web服务以检查是否有更新。 (甚至在后台)

call web service after every 15-20 seconds to check for any update. (Even in Background)

请告知我是否有其他选择可以实现这一目标。

Please advice if I have any other option to achieve this.

推荐答案

我真的不知道您从何处获取信息,但是您和 MD 是错误的,GCM是最佳选择。

I really don't know where you're getting your information from, but both you and MD are wrong and GCM is the best option.

来自您的问题:


<我已计划为此使用推送通知(GCM),但推送
通知将不是一个好选择,因为我不希望用户知道
我们正在向移动应用添加数据。

I planned to use Push Notification (GCM) for this, but push notifications will not be a good option as I didn't want the user to know that we are adding data in mobile app.

GCM与向用户显示通知有关,但这不是它的工作。

GCM is related to showing notifications to the user, but it's not what it does.

GCM是 Google Cloud Messaging。它只会向您的应用发送一条消息。此消息是在 BroadcastReceiver 内部接收的。在此 BroadcastReceiver 内部,您可以执行所需的任何操作,例如将信息与服务器同步。

GCM is "Google Cloud Messaging". It only sends a message to your app. This message is received inside a BroadcastReceiver. From inside this BroadcastReceiver you can execute any actions you need, like syncing the information with your server.

I' ll显示GCM的 BroadcastReceiver 的可能示例实现。

I'll show a possible example implementation of the BroadcastReceiver for the GCM.

这是一个简化的示例,而不是完整的实现:

That's a simplified example and NOT a complete implementation:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle b = intent.getExtras(); // you send those extras from your server
        int type = b.getInt("type");
        switch(type){
            case TYPE_SYNC:
                 // start a `Service` to sync data from your server 
            break;
            case TYPE_ADD_DATA:
                 long id = b.getLong("id");
                 String name = b.getString("name");
                 String descr = b.getString("descr");
                 // call code to add id, name, descr to your local data
            break;
            case TYPE_NOTIFICATION:
                 String title = b.getString("title");
                 String message = b.getString("message");
                 // call code to make a notification with title and message
            break;
        }
    }
}

在此示例中,您的服务器可以发送3种不同类型的GCM。

on this example your server can send 3 different types of GCM.


  • TYPE_SYNC:将使您的应用启动后台服务,该服务将连接到服务器并同步信息

  • TYPE_ADD_DATA:将直接在消息内发送数据,并将其直接添加到设备存储中(可能是SQLite)

  • TYPE_NOTIFICATION:这是唯一的选择用户收到有关任何事情的通知。其他两个选项对用户是透明的。

完整的实现以及如何正确使用 WakefulBroadcastReceiver 请检查官方文档: http://developer.android .com / google / gcm / client.html

for a complete implementation and how to properly use the WakefulBroadcastReceiver please check the official docs: http://developer.android.com/google/gcm/client.html

这篇关于如何将数据从服务器发送到Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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