如何更新通知?

时间:2015-01-14 18:28:54

标签: android notifications broadcast

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

1 个答案:

答案 0 :(得分: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班级

相关问题