java - 从服务中连续运行的线程发布通知

标签 java android android-notifications

我有一个服务,在那个服务中我有一个线程。线程有一个Runnable。从可运行的我尝试发布通知,但我收到此错误。

The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) in the type Notification is not applicable for the arguments (new Runnable(){}, CharSequence, CharSequence, PendingIntent)

如果我尝试从线程外发布,一切正常。 我的应用程序不断从服务器获得响应。当有来自服务器的更新数据并且应用程序未运行时,将创建一个新通知。为了避免在主线程上运行,我创建了一个新线程。

最佳答案

Runnable 中引用 this 并不引用 Context 的实例(您的 ServiceActivity) 但到 Runnable 实例。

尝试以下操作:

final Context context = this;
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        ...
        setLatestInfo(context, charsequence1, charsequence2, pendingIntent);
    }
}

关于java - 从服务中连续运行的线程发布通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17803428/

相关文章:

android - 如何在android中显示多个通知

android - Bundle 中的 getString 丢失旧值

java - 使用 Java 从 javascript 链接获取 html 更改

java - 合并两个 ContentView

android - 有没有办法从以前的 apk 发布文件中获取私有(private)签名的 keystore 文件?

android - 我迷失了,绝望了! Gradle 对它想要什么感到困惑;版本 1.8 还是 1.9?

android - 可以从工作线程调用 NotificationManager.notify() 吗?

java - 数字签名的.keystore文件位置

java - 为什么这个 HashMap.get 返回一个空值?

java - 通过遍历枚举来分配参数