Android通知中的操作:按钮显示三次。
创始人
2024-08-21 20:00:57
0

在Android通知中显示一个按钮并且点击按钮后按钮会显示三次的解决方法如下:

  1. 创建一个扩展通知布局文件(notification_layout.xml)来定义通知的布局:


    
  1. 在创建通知时使用自定义的布局:
// 构建通知的布局
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_layout);

// 创建通知构建器并设置标题、内容和自定义布局
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Notification Title")
        .setContentText("Notification Content")
        .setCustomContentView(notificationLayout);

// 创建通知的操作按钮
Intent buttonIntent = new Intent("button_clicked");
PendingIntent buttonPendingIntent = PendingIntent.getBroadcast(this, 0, buttonIntent, 0);
notificationLayout.setOnClickPendingIntent(R.id.button, buttonPendingIntent);

// 发送通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
  1. 创建一个广播接收器来处理按钮点击事件,并在按钮点击时更新按钮的显示:
public class ButtonClickReceiver extends BroadcastReceiver {

    private static int buttonClickCount = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("button_clicked")) {
            buttonClickCount++;
            if (buttonClickCount <= 3) {
                // 更新按钮的显示
                RemoteViews notificationLayout = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
                notificationLayout.setTextViewText(R.id.button, "Button (" + buttonClickCount + ")");
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
                notificationManager.notify(notificationId, builder.build());
            }
        }
    }
}
  1. 在AndroidManifest.xml文件中注册广播接收器:

    
        
    

以上是在Android通知中显示一个按钮并且点击按钮后按钮会显示三次的解决方法。你可以根据自己的需要进行修改和扩展。

相关内容

热门资讯

推荐透视!werplan怎么作... 推荐透视!werplan怎么作必弊!竟然一直总是有辅助技巧(有挂实锤)-哔哩哔哩;一、werplan...
揭幕透视!pokerworld... 揭幕透视!pokerworld辅助器!切实真的有辅助工具(果真有挂)-哔哩哔哩1、游戏颠覆性的策略玩...
关于透视!epoker底牌透视... 关于透视!epoker底牌透视!好像真的是有辅助技巧(有挂方法)-哔哩哔哩1、epoker底牌透视破...
教你透视!约局吧怎么看有没有挂... 教你透视!约局吧怎么看有没有挂!其实是真的有辅助神器(有挂神器)-哔哩哔哩1、超多福利:超高返利,海...
详情透视!德州来玩辅助器!果然... 详情透视!德州来玩辅助器!果然一直都是有辅助app(确实有挂)-哔哩哔哩1、德州来玩辅助器免费脚本咨...
推荐透视!竞技联盟透视!一直真... 推荐透视!竞技联盟透视!一直真的有辅助软件(有挂教学)-哔哩哔哩1、完成竞技联盟透视辅助器v3.3的...
分享透视!智星菠萝辅助!切实是... 分享透视!智星菠萝辅助!切实是真的有辅助神器(有挂解密)-哔哩哔哩亲,关键说明,智星菠萝辅助透视脚本...
科普透视!xpoker辅助怎么... 科普透视!xpoker辅助怎么用!果然存在有辅助方法(有挂透视)-哔哩哔哩1、超多福利:超高返利,海...
曝光透视!德州私人局怎么透视!... 曝光透视!德州私人局怎么透视!本来真的是有辅助app(有挂技巧)-哔哩哔哩1)德州私人局怎么透视有没...
曝光透视!智星德州可以透视吗!... 曝光透视!智星德州可以透视吗!都是一直都是有辅助技巧(真是有挂)-哔哩哔哩1、金币登录送、破产送、升...