从通知打开对话框 [英] Open Dialog from Notification

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

问题描述

我显示了一条通知.现在我希望这发生:

I have a notification displayed. Now I want this to happen:

当我点击通知时,我想打开一个对话框,在那里我只打印 1 个字符串.

When I click on the notification, I would like to open a Dialog, where I print only 1 string.

现在,我无法解决,当我创建通知时该怎么做:

Now, I can't work it out, what to do here, when I create notification:

...
Intent notificationIntent = new Intent(context, {how to open dialog}); 
...

然后是 1 个按钮,例如确定",这将关闭对话框.

Then 1 button, for example "OK", which will close the dialog.

请帮帮我.

谢谢.

推荐答案

我在我的一个应用程序中就是这样做的.在通知中,您需要执行以下操作:

I do exactly this in one of my apps. In the notification you need to do something like this:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    new Intent("com.yourcompany.yourapp.MAINACTIVITY").putExtra("fromnotification", true);

在您的主要活动中使用 onResume() 方法来检查这个额外的:

Inside your main activity use the onResume() method to check for this extra:

@Override
    public void onResume()
    {
            super.onResume();

            if (getActivity().getIntent().getBooleanExtra("fromnotification", false) == true)
            {
                    getActivity().getIntent().removeExtra("fromnotification");
                    startActivityForResult(
                                    new Intent("com.yourcompany.yourapp.DIALOGACTIVITY"), 123);
            }

    }

此代码显示一个具有对话框样式的活动,但没有理由不能在 if 语句中创建对话框.

This code displays an activity with a dialog style, but there is no reason why it can't create a dialog inside the if statement though.

这篇关于从通知打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆