android - 使用 Timer 从 android 做 http 请求

标签 android multithreading

我想写安卓聊天应用,我想知道 定期向网络服务器发出请求以获得 通过在

中打开新的 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/

相关文章:

android - ScrollView 内的 ImageView 不会调整大小以填满屏幕(保持纵横比)

C++ 单线程工作,在新线程上它调用 "abort()"

Java 易失变量、多线程

android - 如何在服务类中使用 Volley ?

android.database.sqlite.SQLiteException : near "?": syntax error (code 1):

android - 了解 Gradle (Android Studio) 有多重要?

java - 在 Android 4 上自定义 ActionBar 选项卡

android - onUserInteraction() 到底什么时候被调用?

python - 在 Python 中检查当前线程是否是主线程

java - Java线程和异常?