如何在第二次设置通知的时间和日期?

时间:2017-09-11 09:00:16

标签: java android push-notification

.setWhen(System.currentTimeMillis()) 

是在推送通知android中设置时间的代码,结果类似于15.54。但是,如果我想用秒时间设置这个15:54:02的时间,我该怎么办?你能帮帮我或提供示例源代码吗?

这是我的通知管理器源代码。

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
    Notification notification;
    notification = mBuilder.setSmallIcon(R.drawable.logo).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent)
            .setContentTitle(title)
            .setSmallIcon(R.drawable.logo)
            .setWhen(System.currentTimeMillis())
            .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
            .setContentText(message)
            .build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;

    notification.defaults = Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults = Notification.DEFAULT_ALL;
    int id = (int) System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
    notificationManager.notify(id, notification);
}

4 个答案:

答案 0 :(得分:1)

  

但如果我想像这样设定时间15:54:02

内置通知布局无法实现这一点。如果您真的需要秒,则需要自定义通知布局。但我强烈反对这一点。

答案 1 :(得分:1)

你设置或不设置毫秒.setWhen()默认情况下它会得到。

时间格式您希望显示的内容如15:54:02 24小时格式与秒。时间格式NotificationCompat.Builder类没有设置。

您设置转换为时间的毫秒数,以便在15:54这样的通知中显示,而不是秒。

您可以使用自定义通知视图,而不是默认视图。

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    String formattedDate = "";
    formattedDate = sdf.format(new Date());

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
    contentView.setImageViewResource(R.id.image, R.drawable.logo);
    contentView.setTextViewText(R.id.title, title);
    contentView.setTextViewText(R.id.text, message);
    contentView.setTextViewText(R.id.time, formattedDate);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.home)
            .setContent(contentView);

    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    NotificationManager notificationManager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
    int id = (int) System.currentTimeMillis();
    notificationManager.notify(id, notification);

布局..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:gravity="left|center_horizontal|center_vertical"
          android:weightSum="4">

<RelativeLayout
      android:id="@+id/layout"
      android:layout_width="0dp"
      android:layout_weight="3"
      android:layout_height="64dp"
      android:padding="10dp" >
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView
        android:textSize="13dp"
        android:textColor="#000"
        android:text="Testing"
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        />
    <TextView
        android:textSize="13dp"
        android:textColor="#000"
        android:text="Testing is awecome"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        />
</RelativeLayout>

<TextView
    android:textSize="13dp"
    android:textColor="#000"
    android:text="Testing"
    android:id="@+id/time"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:paddingRight="20dp"
    android:gravity="right|center_vertical|center_horizontal"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"/>

  </LinearLayout>

像这样输出......

enter image description here

答案 2 :(得分:0)

为此,您需要以毫秒为单位转换时间“14:54:02”并将结果时间设置为.setWhen()

答案 3 :(得分:-1)

SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss"); 

String dateString = formatter.format(new Date(dateInMillis)));