2017-11-03 4 views
-1

Ich handle Android benutzerdefinierte Benachrichtigung. Ich wusste, dass NotificationCompat.Builder eine Methode mit dem Namen 'setShowWhen (boolean)' hat, um den Benutzern zu zeigen, dass die Benachrichtigung kam.Wie kann man die Benachrichtigungszeit auf RemoteViews anzeigen?

Aber ich kann nicht den gleichen Weg auf RemoteViews zu tun, die eine benutzerdefinierte Benachrichtigungsansicht ist. Kannst du mir helfen, den Weg zu finden, wie man die Zeit anzeigt? Danke.

Antwort

0

siehe unten Code.

RemoteViews remoteViews = new RemoteViews(getPackageName(), 
      R.layout.customnotification); 

    // Set Notification Title 
    String strtitle = getString(R.string.customnotificationtitle); 
    // Set Notification Text 
    String strtext = getString(R.string.customnotificationtext); 

    // Open NotificationView Class on Notification Click 
    Intent intent = new Intent(this, NotificationView.class); 
    // Send data to NotificationView Class 
    intent.putExtra("title", strtitle); 
    intent.putExtra("text", strtext); 
    // Open NotificationView.java Activity 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      // Set Icon 
      .setSmallIcon(R.drawable.logosmall) 
      // Set Ticker Message 
      .setTicker(getString(R.string.customnotificationticker)) 
      // Dismiss Notification 
      .setAutoCancel(true) 
      // Set PendingIntent into Notification 
      .setContentIntent(pIntent) 
      // Set RemoteViews into Notification 
      .setContent(remoteViews); 

    // Locate and set the Image into customnotificationtext.xml ImageViews 
    remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher); 
    remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy); 

    // Locate and set the Text into customnotificationtext.xml TextViews 
    remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle)); 
    remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext)); 

    // Create Notification Manager 
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    // Build Notification with Notification Manager 
    notificationmanager.notify(0, builder.build()); 
+0

netter Code, aber es gibt keine Teile, um 'Zeit' zu behandeln. – eyeballs

+0

Sie müssen Textansicht als Zeit im Layout einfügen und dann Zeit von Datum() nehmen und es einstellen. – Vasant

+0

in meinem Code benutzerdefinierte Layout ist customnotification. – Vasant

Verwandte Themen