迷失在 PendingIntent 字段中

发布于 2024-11-16 05:54:11 字数 1567 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-11-23 05:54:11

解决了
需要做的是在意图中使用 Neglected.class。

solved
What needed to be done, is use the Neglected.class in the intent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文