2017-09-11 1 views
0
.setWhen(System.currentTimeMillis()) 

est le code pour définir l'heure dans la notification push android et le résultat est comme 15.54. Mais si je veux mettre le temps comme ça 15:54:02 avec secondes, que dois-je faire? Pouvez-vous m'aider ou me fournir un exemple de code source?Comment puis-je régler l'heure et la date de notification une seconde fois?

Ceci est mon code source de notification-manager.

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); 
} 
+0

Ce tripoter 'notification.defaults' semble me non-sens. Je voterais pour enlever cela pour ne pas dérouter les lecteurs, à la fois ici et dans la base de code réelle. –

Répondre

0

Vous définissez ou non milliseconde avec .setWhen() par défaut, il obtient cela.

Format de l'heure ce que vous voulez afficher comme 15:54:02 format 24 heures avec seconde. Il n'y a pas de paramètres pour la classe de format d'heure NotificationCompat.Builder.

Quoi que vous définissiez la milliseconde qui convertissent à temps pour afficher dans la notification comme 15:54 sans seconde.

Ce que vous pouvez faire est d'utiliser la vue Notification personnalisée plutôt que la vue par défaut.

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

.. Mise en page

<?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> 

sortie comme ça ...

enter image description here

+0

merci, je vais essayer –

+0

vous êtes le bienvenu .. –

1

Mais si je veux régler le temps comme celui-ci 15:54:02

Vous ne pouvez pas avoir qu'avec intégré la mise en page de notification. Si vous avez vraiment besoin de secondes, vous devez disposer d'une disposition de notification personnalisée. Mais je décourage fortement cela.

+0

mais j'en ai besoin (la deuxième fois) son pas de problème si place dans mon message pas dans mon avis. J'ai seulement besoin quand ma notification a reçu dans mon téléphone dans la deuxième fois. parce que mon programme envoie une notification pour la surveillance du réseau. Je veux analyser l'heure d'envoi du serveur et ensuite recevoir dans mon smartphone. ou avez-vous une suggestion sur comment compter retarder ma notification? –

0

pour cela, vous devez convertir ce temps « 14:54:02 » en millisecondes et que le temps de régler résultat dans .setWhen()

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

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