Android的通知,上面有按钮 [英] Android notification with buttons on it

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

问题描述

我想发出通知,2个按钮就可以了:

I'm trying to make a notification with 2 buttons on it:

  • 一个把我带回到活动
  • 其他关闭它

有没有人有关于如何捕捉到按钮单击事件的想法(记住,活动已暂停)?

Has anyone got an idea on how to catch the button click event (remember that the Activity is paused)?

推荐答案

我很高兴将它张贴!工作了一晚上后,我发现了什么。所以,在这里,我们走!

I am glad to post it! After working all night I found something. So, here we go!

1。创建您的通知一个XML布局文件。

2。创建使用Notification.Builder通知。将一切后,你想要的(图标,声音等)做到这一点:

        //R.layout.notification_layout is from step 1

        RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout);

        setListeners(contentView);//look at step 3

        notification.contentView = contentView;

3。创建方法setListeners。在此方法中,你必须这样写:

    //HelperActivity will be shown at step 4

    Intent radio=new Intent(ctx, packagename.youractivity.class);  
    radio.putExtra("AN_ACTION", "do");//if necessary

    PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
    //R.id.radio is a button from the layout which is created at step 2  view.setOnClickPendingIntent(R.id.radio, pRadio); 

    //Follows exactly my code!
    Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
    volume.putExtra("DO", "volume");</p>

    //HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
    PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
    view.setOnClickPendingIntent(R.id.volume, pVolume);

4。对于我的要求,我用了一个HelperActivity这是为了响应的意图。但是,对于你,我不认为这是必要的。

如果你想完整的源$ C ​​$ C,你可以浏览它,或者从我的混帐回购协议下载。在code是供个人使用的,所以不要指望读了华丽code有很多评论。 <一href="https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts">https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

If you want the full source code you can browse it or download it from my git repo. The code is for personal use, so don't expect to read a gorgeous code with a lot of comments. https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

所有上述要求,回答捕事件从不同的按钮的问题。

ALL THE ABOVE, ANSWERS THE QUESTION OF CATCHING EVENT FROM DIFFERENT BUTTONS.

关于取消通知,我在这里您重定向

About canceling the notification I redirect you here

如何清除Android的通知

不过,别忘了用你解析的通知方法,当你打电话通知了拳头时间ID

Just remember to use the id you parsed at the notify method when you called the notification for fist time

这篇关于Android的通知,上面有按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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