从通知启动服务 [英] Start Service from Notification

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

问题描述

是否可以从通知中启动服务.启动 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屋!

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