gpt4 book ai didi

带有按钮的 Android 通知

转载 作者:IT老高 更新时间:2023-10-28 23:27:18 26 4
gpt4 key购买 nike

我正在尝试用 2 个按钮发出通知:

  • 一个人带我回到 Activity 中
  • 另一个关闭它

有没有人知道如何捕捉按钮点击事件(记住 Activity 已暂停)?

最佳答案

我很高兴发布它!工作了一夜之后,我发现了一些东西。所以,我们开始吧!

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.对于我的要求,我使用了一个响应 Intent 的 HelperActivity。但对你来说,我认为没有必要。

如果你想要完整的源代码,你可以浏览它或者从我的 git repo 下载它。该代码是供个人使用的,所以不要期望阅读带有大量注释的华丽代码。 https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

以上所有内容,回答了从不同按钮捕获事件的问题。

关于取消通知我把你重定向到这里

How to clear a notification in Android

记得在第一次调用通知时使用你在通知方法中解析的id

关于带有按钮的 Android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10516722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com