如何在RemoteViews上显示通知时间?

时间:2017-11-03 04:45:37

标签: android

我正在处理Android自定义通知。 我知道NotificationCompat.Builder有一个名为'setShowWhen(boolean)'的方法来向用户显示通知何时到来。

但我找不到在RemoteViews上做同样事情的方法,这是一个自定义通知视图。 你能帮我找到如何显示时间的方法吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

见下面的代码。

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