【问题标题】:how to update notification?如何更新通知?
【发布时间】:2015-01-14 18:28:54
【问题描述】:

我有一个带有文本视图和按钮的自定义通知。我想在单击按钮时更改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 但我不知道如何将此更改更新到通知面板。

【问题讨论】:

  • 你只需要使用相同的notificationID发布一个新的通知

标签: android notifications broadcast


【解决方案1】:

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

    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);

另外,如果你想添加你的远程视图,你可以使用方法:

setContent(RemoteViews remoteView)

来自Notification.Builder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多