我对 PendingIntent 有点迷茫。
据我所知,它是给操作系统以执行稍后(因此挂起)操作的令牌。
我有一项启动服务的活动。该服务偶尔会创建一个通知。我想要做的,最简单的,就是把活动带到最前面。
我不确定我在哪里以及如何创建以及我将 PendingActivity 发送给谁。
- 如果我在 Activity 中创建它,我需要将它发送到服务 - 如何?
- 如果我在服务中创建它,上下文将如何调用活动?这些是一样的吗?- 我虽然这些与操作系统的工作方式相同,但它对我不起作用。
这是一些代码行
顺便说一句,这不起作用 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);