带有按钮的 Android 通知 [英] Android notification with buttons on it
问题描述
我正在尝试制作带有 2 个按钮的通知:
I'm trying to make a notification with 2 buttons on it:
- 一个带我回到活动中
- 另一个关闭它
有没有人知道如何捕捉按钮点击事件(记住 Activity 是暂停的)?
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.但对你来说,我认为没有必要.
如果你想要完整的源代码,你可以浏览它或从我的 git repo 下载它.代码仅供个人使用,因此不要期望阅读带有大量注释的华丽代码.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
记得使用你第一次调用通知时在notify方法解析的id
Just remember to use the id you parsed at the notify method when you called the notification for fist time
这篇关于带有按钮的 Android 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!