我有一个服务,在那个服务中我有一个线程。线程有一个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
的实例(您的 Service
或 Activity
) 但到 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/