Open Dialog from Notification

元气小坏坏 提交于 2019-12-30 02:17:08

问题


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

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}); 
...

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

Please, help me.

Thanks.


回答1:


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);

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);
            }

    }

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.



来源:https://stackoverflow.com/questions/10902435/open-dialog-from-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!