gpt4 book ai didi

android - 如何更新通知?

转载 作者:行者123 更新时间:2023-11-30 02:25:27 27 4
gpt4 key购买 nike

我有一个带有 TextView 和按钮的自定义通知。我想在单击按钮时更改 TextView 的文本。我做了一个广播,当点击按钮时程序开始运行那个广播,广播的代码是:

package com.mori.sepid.notifications;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.ViewGroup;
import android.app.Notification;
import android.widget.RemoteViews;
import android.app.NotificationManager;
import android.widget.TextView;

public class button_broadcast_resiver extends BroadcastReceiver {
public button_broadcast_resiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.customnoti1);
remoteViews.setTextViewText(R.id.text,"Hi morteza");
NotificationManager noti=
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

}


}

正如您在上面看到的代码,我从 RemoteView 创建了一个对象并将选定的文本放入 TextView,但我不知道如何将此更改更新到通知面板。

最佳答案

下面的代码解释了如何创建您自己的通知:

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new NotificationCompat.Builder(context)
//here you set icon which will be displayed on top bar
.setSmallIcon(R.drawable.your_ic_notification)
//here is title for your notification
.setContentTitle("tite"/*your notification title*/)
//here is a content which will be displayed when someone expand action bar
.setContentText("Some example context string"/*notifcation message*/)
.build();
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(1000/*some int notification id*/, notification);

此外,如果你想添加你的远程 View ,你可以使用方法:

setContent(RemoteViews remoteView)

来自 Notification.Builder

关于android - 如何更新通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27949959/

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