简体   繁体   中英

Send data from a web server to an android app

I'm trying to understand how I can send data from a web server to my android app. This isn't a request/receive type question, what I am looking to do is basically have my web server (say for example www.example.com/index.php) send a data (like a string for example) to my android app. Now, this data can be sent anytime, it isn't something where a you click something on the app to receive the data, but rather, when the server is wanting to send data, it sends the data, and my app picks it up. Now I'm not looking for my app to be listening for this data every second, an interval of 5 minutes or so is fine.

The question being, how would I achieve something like this? How can I send a string containing "Hello World" to my app? I know PHP, and I know this has to be done using POST/GET, and of course I know there will probably be some type of listener, service, or something of sort on the android side listening for this data, but I just want to know how I can approach this. What should my setup be?

If someone can provide some link or code to send a basic String message, then that would be more than enough for me to learn from.

There is two best approach you can follow.

  1. from server to app: in this case make corn job(timer service) which send push notification to android app. when app get push notification create one background service which download any data from server.

Note: if you have small text with limit of 100 char then no need to create service just send using push notification

  1. fetch data from server: in this case make one background service(auto start service) in android app. which run within some interval period like 1 hour or more(whatever your requirement). that service fetch data from server. (in this case android app eat battery of user device because of background service)

i will suggest you to use 1st approach because its easy and best way(in case of performance) your server do everything & android app have to just receive data.


how to implement push notification best tutorial using PHP & android(its old but just read official docs )

为此,您需要实现android push通知。当您从php服务器发送数据时,应用程序会自动获取此数据。

是的,如果您希望将数据从服务器端发送到应用程序中而无需在单击按钮时调用Web服务,则将使用cronjob,它在服务器端定义的时间运行(即:如果您想每五秒或五分钟运行一次您可能不需要从应用程序端调用它)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM