简体   繁体   中英

How to link Android application and web-service?

I made an Android application and I have a web-service (written in PHP). The service must send a message to application, so while my Android application works in the background and monitors web-service, it gets the message from service.

How can I organize this interaction? Thank you

C2DM is probably the most efficient way to push notifications to your android phone (no extra polling needed). Give it a look.

If you want to do it with the background service, you can set the service to be woken up every x minutes with AlarmManager and send http request to your server to check for updates. However this does consume battery if you do it too often.

There are many examples on the web with a quick google search or even on here.

A couple of samples:

http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/ http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient http://www.anddev.org/calling_a_web_service_from_android-t348.html

Remember that anything that could take time should be run in a thread outside of the UI thread

I'd suggest using a Handler Please have a read of this performance article

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