我想写安卓聊天应用,我想知道 定期向网络服务器发出请求以获得 通过在
中打开新的 AsynkTask 线程使用 android Timer 发送新消息private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
方法?
谢谢。
最佳答案
为什么不呢,但是如果您希望您的聊天应用程序能够在屏幕关闭时向用户发送新消息通知,那么您可以在服务中实现计时器,也许更多是通过设置定期请求的警报服务器:
- 设置警报(不精确重复)以向广播接收器发送 Intent 。
- 广播接收器启动服务。
- 服务启动一个 AsyncTask 来请求网络服务器。
- 如果有任何新消息,服务会向用户发送(或不发送)通知。
- 服务自行停止。
关于android - 使用 Timer 从 android 做 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7045300/