从通知启动服务

编程入门 行业动态 更新时间:2024-10-26 08:23:34
本文介绍了从通知启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从通知中启动服务.启动 Activity 的正常方式工作正常,但在实际启动应用程序之前,我需要对数据进行一些预检查.

is it possible to start a service from a notification. The normal way of starting an activity is working perfectly, but I need some pre checks of data before actually starting the app.

我已经通过在通知 Intent 中包含有效服务对其进行了测试,但没有任何反应.

I've tested it with including a valid service in the notification intent, but nothing happens.

推荐答案

可以从通知中启动服务.

It is possible to start a service from a notification.

您必须使用 PendingIntent.getService 而不是 pendingIntent.getActivity.

You have to use PendingIntent.getService instead of pendingIntent.getActivity.

Intent notificationIntent = new Intent(mContext, HandleNotificationClickService.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0);

Notification notification = new Notification(icon, tickerText,System.currentTimeMillis());
notification.setLatestEventInfo(mContext,contentTitle , contentText, pendingIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(CALLER_ID_NOTIFICATION_ID, notification);

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

更多推荐

[db:关键词]

本文发布于:2023-03-30 21:10:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/787582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!