迷失在 PendingIntent 字段中
我对 PendingIntent 有点迷失。
据我所知,它是给予操作系统的令牌,用于执行稍后(因此待处理)的操作。
我有一项启动服务的活动。该服务偶尔会创建通知。 我想做的最简单的事情就是将活动带到前台。
我不确定在哪里、如何创建以及将 PendingActivity 发送给谁。
- 如果我在活动中创建它,我需要将其发送到服务 - 如何?
- 如果我在服务中创建它,上下文将如何调用该活动?这些是一样的吗? - 我虽然这些与操作系统的工作方式相同,但它对我不起作用。
以下是一些代码行,
这在 StartService 获取 Intent 时不起作用。 这段代码在我的活动中
Intent intent = new Intent(this, NeglectedService.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_ONE_SHOT);
startService(contentIntent);
所以,正确的代码是
Intent intent = new Intent(this, NeglectedService.class);
startService(contentIntent);
所以我想在我的服务中制作挂起的意图,但这对我不起作用,因为我不确定如何重用/使用意图
Notification notification = new Notification(R.drawable.icon,
extra,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent, // not sure what intent to use here !!!!
PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_INSISTENT;
mNotificationManager.notify(id, notification);
I am a bit lost with PendingIntent.
As far as I could understand, it's a Token given to the OS to perform later (hence pending) operations.
I have an activity that launched a service. The service, occasionally creates a notification.
What I am trying to do, as the simplest of all, is to bring the activity to the front.
I am not sure where and how I create and to whom I send the PendingActivity.
- If I create it within the Activity, I need to send it to the service - HOW?
- If I create it within the service, how would the context be to call the activity? are these the same? - I though these are the same, as how the OS works, but it did not work for me.
Here are some code lines
This is not working btw StartService gets an Intent.
This code is in my activity
Intent intent = new Intent(this, NeglectedService.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_ONE_SHOT);
startService(contentIntent);
So, the correct one is
Intent intent = new Intent(this, NeglectedService.class);
startService(contentIntent);
So I think to make the pending intent in my service, but this didn't work for me, as I am not sure how to reuse/use the intent
Notification notification = new Notification(R.drawable.icon,
extra,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this,
0,
intent, // not sure what intent to use here !!!!
PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_INSISTENT;
mNotificationManager.notify(id, notification);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了
需要做的是在意图中使用 Neglected.class。
solved
What needed to be done, is use the Neglected.class in the intent.