我很高兴发布它!经过一夜的工作,我发现了一些东西。那么,我们开始吧!
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 存储库下载。该代码仅供个人使用,因此不要指望阅读带有大量注释的华丽代码。https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts
以上,回答了从不同按钮捕获事件的问题。
关于取消通知,我将您重定向到此处
如何清除 Android 中的通知 https://stackoverflow.com/questions/2665634/how-to-clear-a-notification-in-android
只需记住使用您第一次调用通知时在通知方法中解析的 id