从 Android 中的服务发送通知 [英] Sending a notification from a service in Android
本文介绍了从 Android 中的服务发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一项服务正在运行,我想发送通知.太糟糕了,通知对象需要一个 Context
,就像一个 Activity
,而不是一个 Service
.
I have a service running, and would like to send a notification. Too bad, the notification object requires a Context
, like an Activity
, and not a Service
.
你知道有什么方法可以绕过吗?我试图为每个通知创建一个 Activity
但它看起来很丑,而且我找不到一种方法来启动 Activity
没有任何 View
.
Do you know any way to by pass that ? I tried to create an Activity
for each notification but it seems ugly, and I can't find a way to launch an Activity
without any View
.
推荐答案
Activity
和 Service
实际上都是 extend
Context
所以你可以简单地使用 this
作为您的 Service
中的 Context
.
Both Activity
and Service
actually extend
Context
so you can simply use this
as your Context
within your Service
.
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = /* your intent */;
notification.setLatestEventInfo(this, /* your content */, pendingIntent);
notificationManager.notify(/* id */, notification);
这篇关于从 Android 中的服务发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文