从Android的服务发送通知 [英] Sending a notification from a service in Android

查看:353
本文介绍了从Android的服务发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务运行,并想发送通知。太糟糕了,通知对象需要一个背景下,像一个活动,而不是服务。

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.

你知道什么方法可以通过传递?我想卜似乎丑陋创建活动的每个通知,我不能找到一种方法来启动一个活动,没有任何看法。

Do you know any way to by pass that ? I tried to create an Activity for each notification bu it seems ugly, and I can't find a way to launch an Activity without any view.

推荐答案

两个活动服务实际上延长 上下文 ,所以你可以简单地使用作为上下文服务范围内

Both Activity and Service actually extend Context so you can simply use this as your Context within your Service.

NotificationManager notificationManager =
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(/* your notification */);
PendingIntent pendingIntent = /* your intent */;
notification.setLatestEventInfo(this, /* your content */, pendingIntent);
notificationManager.notify(/* id */, notification);

这篇关于从Android的服务发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆