从开始通知服务 [英] Start Service from Notification

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

问题描述

,才有可能从通知启动服务。 开始活动的正常方式工作的很好,但我在实际开始应用程序需要的数据约pre检查。

我已经与包括在通知的意图有效的服务进行了测试,但没有任何反应。

解决方案
  

有可能从通知启动服务。

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

 意图notificationIntent =新的意图(mContext,HandleNotificationClickService.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext,0,notificationIntent,0);

通知通知=新的通知(图标,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,通知);
 

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.

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.

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天全站免登陆