如何实现后台监听套接字为Android? [英] How to implement a background socket listener for Android?

查看:101
本文介绍了如何实现后台监听套接字为Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序监听通过套接字JSON命令。我想知道我怎么能实现这个作为后台服务。该服务将接收的命令,并根据哪些命令是这样,通知主程序内的用户,或更新数据。有些code的例子是巨大的,如果任何人有他们。我有一个如何打造但是后台服务,它使用定时器的例子。我想对于插座总是在听。

I have an android app that listens for json commands over a socket. I am wondering how I can implement this as a background service. The service would receive the commands and depending on which commands they are, notify the user, or update data within the main program. Some code examples would be great if anyone has them. I've got an example of how to build a background service however it uses timers. I'd like for socket to always be listening.

推荐答案

在这种情况下,你可以使用一个部门,它响应发送过来的套接字事件。

In this case you would use a Service which responds to events sent over the socket.

该服务应该由活动开始(也可以在需要时开机启动)。

The service should be started by the activity (could also be started on boot if needed).

有关code的例子,我会建议 commonwares在GitHub上项目。有使用服务有很多很好的例子。

For code example I would recommend commonwares projects on GitHub. There are many great examples using Services there.

(也有在一个持续的应用服务的例子,我有<一个href="http://$c$c.google.com/p/android-bluetooth-on-motion/source/browse/trunk/BluetoothOnMotion/src/com/elsewhat/android/onmotion/BluetoothOnMotionService.java"相对=nofollow>这里。)

(There is also a service example in an ongoing app I have here.)

这篇关于如何实现后台监听套接字为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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