来自服务的通知有效,但活动尚未启动

发布于 2024-12-16 13:57:21 字数 893 浏览 1 评论 0 原文

我正在尝试从服务发送通知,并且成功(通知确实显示在状态栏上)。 但是,在单击通知以观看活动后 - 我将返回移动主屏幕。此外,我注意到在调试模式下我没有到达活动的“onCreate”方法。

这是我的代码: 服务代码:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int icon = R.drawable.notificationicon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, NotificationsActivity.class), 0);
    notification.setLatestEventInfo(this, "My notification","Hello World!", pendingIntent);
    notificationManager.notify(1, notification);

活动代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(dialogLayout);
}

I'm trying to send a notification from a service, and does that succeessfully (the notification does show on the status bar).
BUT, after the click on the notification in order to watch the activity - I'm going back to the mobile home screen. In addition, I've noticed in debug mode that i'm not reaching to the activity's "onCreate" method.

here is my piece of code:
The service code:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int icon = R.drawable.notificationicon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, NotificationsActivity.class), 0);
    notification.setLatestEventInfo(this, "My notification","Hello World!", pendingIntent);
    notificationManager.notify(1, notification);

Activity code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(dialogLayout);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

赠佳期 2024-12-23 13:57:21

如果您想从接收器/服务中startActivity,您必须创建一个意图,添加一个标志,通知它启动一个新任务,然后启动该活动。

适应这个伪代码:

Intent startServiceIntent = new Intent(context, com.myCorp.myApp.myClass.class);
startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startServiceIntent);

我在 BootReceiver 中使用它来启动应用程序(例如使用此 BootReceiver)。

in case you want to startActivity from within a receiver / service, you must create an intent, add a flag notifying it to start a new task and THEN start the activity.

adapt this pseudo-code:

Intent startServiceIntent = new Intent(context, com.myCorp.myApp.myClass.class);
startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startServiceIntent);

i use this in a BootReceiver to start up an application (for example use this BootReceiver).

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